0

I want to show multiple markers on my Google map with OpenLayers. Below is my code :

for (var pointcount = 0; pointcount < jsonObj.length; pointcount++) {

if (jsonObj[pointcount].Long != null && jsonObj[pointcount].Lat != null) {

    var markerslonLat = new OpenLayers.LonLat(jsonObj[pointcount].Long, jsonObj[pointcount].Lat).transform(WGS84, map.getProjectionObject(), 0);
    map.panTo(markerslonLat);

    markers.addMarker(new OpenLayers.Marker(markerslonLat, icon));

    alert('  marker added. Long : ' + jsonObj[pointcount].Long + '    ||    lat : ' + jsonObj[pointcount].Lat);
}

But, it shows only last one. What have I missed in my code?

2

I have a feeling that your icon is being reused and that is why it appears that only the last point is showing up, it is the only one with an icon. After the icon is used once, you need to clone your icon.

http://dev.openlayers.org/docs/files/OpenLayers/Marker-js.html

so you should have something like this:

if(pointcount == 0){
    markers.addMarker(new OpenLayers.Marker(markerslonLat, icon));
}
else{
    //Makes another icon to use by cloning.
    markers.addMarker(new OpenLayers.Marker(markerslonLat, icon.clone()));
}

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.