I have multiple asset icons on a map

I want to be able to zoom to show all icons on the map I have the Lat and Long of each asset

Currently, i have it working with 1 asset as I can use this code to zoom to the asset map.setCenter(new OpenLayers.LonLat(obj.d[k].vLongitude, obj.d[k].vLatitude).transform(WGS84, map.getProjectionObject()), map.getZoom());

Do I need to pass in an array of co-ordinates?


create coordinate list

var obj={};
obj.d[0] ={};
obj.d[0].vLongitude = 45; 
obj.d[0].vLatitude= 45; 
obj.d[1] ={};
obj.d[1].vLongitude = 55; 
obj.d[1].vLatitude= 55;

create a markers layer

var markers = new OpenLayers.Layer.Markers( "Markers" );

and add all markers on map

for(var i=0;i<obj.d.length;i++){ 
 var size = new OpenLayers.Size(25,25);
            var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
            var icon = new OpenLayers.Icon('http://www.openlayers.org/api/img/zoom-world-mini.png',size,offset);
            markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(obj.d[i].vLongitude,obj.d[i].vLatitude),icon));


zoom to data extent

  • You could do this with a list and append. Creating a dict for a lat/lon pair is kind of overkill, though the overall approach of getDataExtent is good. – John Powell Nov 6 '14 at 13:47
  • Thanks! I'm been struggling to get this working Its putting an icon in the sea in Gulf of Guinea, not sure what thats all about! – user8297 Nov 10 '14 at 17:48
  • using dev tools in chrome i can see the markers:array has 3 objects and maxExtent has the same values for top, right, bottom, left: -20037508.34 – user8297 Nov 10 '14 at 18:00
  • ok kinda got it working, but i'm not sure how to get rid of the marker... it seems i'm using vectors and not markers, dont suppose you have a VECTOR example? – user8297 Nov 11 '14 at 14:30
  • hi, getDataExtent function is avaible to vector layer. more information is dev.openlayers.org/docs/files/OpenLayers/Layer/… and add feature in vector layer dev.openlayers.org/docs/files/OpenLayers/Layer/… example : var vector= new OpenLayers.Layer.Vector( "Vector" ); map.addLayer(vector); for(var i=0;i<obj.d.length;i++){ vector.addFeatures([ new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(obj.d[i].vLongitude,obj.d[i].vLatitude))]) } – emredelioglu Nov 13 '14 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.