Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

If I have a object containing lats and longs how can i plot them in leaflet using the marker constructor I tried looping through them

for(key in obj){
            var val = obj[key];
            var markers = new L.Marker([val.lat, val.long]);
            map.addLayer(markers);
        }

but its not showing up in the map

var obj ={
 0 : {
        lat: 15.606562000000000
        long: 32.513681000000000
    },
1 : {
        lat: -12.09137
        long: 34.7182
    }
}
share|improve this question
add comment

1 Answer 1

No, you're not using the L.Marker constructor.

Constructor: L.Marker( <LatLng> latlng, <Marker options> options? )

And a L.LatLng is:

Constructor: L.LatLng( <Number> latitude, <Number> longitude)

So:

var markers = new L.Marker(new L.LatLng(val.lat, val.long));

Additional JS tips:

  1. markers should be marker, as it is a reference to a single marker.
  2. If your indices are incremental numbers, you should consider making obj an array, and use a for loop instead of a for-in. If not, use hasOwnProperty to avoid nasty surprises.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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