Sunny Sunny - 4 months ago 10
HTML Question

How to show polyline using OpenLayer

Using the following code I get my desired result i.e I get the polyline on my given latitude, longitude

<html>
<head>
<title>OpenLayers Example</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
</head>
<body>
<div style="width:100%; height:100%" id="map"></div>
<script defer="defer" type="text/javascript">
var map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );



var start_point = new OpenLayers.Geometry.Point(0,10);
var start_point_1= new OpenLayers.Geometry.Point(0,15);
var start_point_2= new OpenLayers.Geometry.Point(5,20);
var end_point = new OpenLayers.Geometry.Point(30,0);
var end_point_1 = new OpenLayers.Geometry.Point(40,0);

var myTrip =[start_point, start_point_1, start_point_2 , end_point, end_point_1];
var vector = new OpenLayers.Layer.Vector();
vector.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(myTrip ))]);
map.addLayers([vector]);


map.addLayers([wms]);
map.zoomToMaxExtent();
</script>

</body>
</html>


Output

enter image description here

But when I am using google map it's not working i.e it's just showing me simple map.

<html>
<head>
<title>OpenLayers Example</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="http://maps.google.com/maps/api/js?v=3.2&sensor=false"></script>
</head>
<body>
<div style="width:100%; height:100%" id="map"></div>
<script defer="defer" type="text/javascript">
var map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.Google(
"Google Streets", // the default
{numZoomLevels: 20}
);

var lonlat = new OpenLayers.LonLat(73.589,19.54).transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator
);

var zoom = 7;



var start_point = new OpenLayers.Geometry.Point(0,10);
var start_point_1= new OpenLayers.Geometry.Point(0,15);
var start_point_2= new OpenLayers.Geometry.Point(5,20);
var end_point = new OpenLayers.Geometry.Point(30,0);
var end_point_1 = new OpenLayers.Geometry.Point(40,0);

var myTrip =[start_point, start_point_1, start_point_2 , end_point, end_point_1];
var vector = new OpenLayers.Layer.Vector();
vector.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(myTrip ))]);
map.addLayers([vector]);


map.addLayer(wms);
map.zoomToMaxExtent();
map.setCenter(lonlat, zoom);
</script>

</body>
</html>


output

enter image description here

Why is this not working ,How can I make this work. If instead of openlayer I directly use Google Map API I can get the desired result.

Answer

Google map uses Mercator (EPSG:900913) coordinates, but your geometry is in unprojected geographic coordinates (EPSG:4326). As a result, that line will be extremly small dot in equator.

You should transform it to Mercator (same thing is allready done with lonlat variable):

var myTrip =[start_point, start_point_1, start_point_2 , end_point, end_point_1];
var vector = new OpenLayers.Layer.Vector();
var geom = new OpenLayers.Geometry.LineString(myTrip);
geom = geom.transform(
    new OpenLayers.Projection("EPSG:4326"),
    new OpenLayers.Projection("EPSG:900913")
);
vector.addFeatures([new OpenLayers.Feature.Vector(geom)]);
map.addLayers([vector]);

Pay attention, that this linestring will be placed to Africa, but your map will be zoomed to India. To see that line, you have to zoom out your map from initial view.

Comments