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.

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 i missed in code. Any help would be appreciated. :) Thanks.

share|improve this question

1 Answer 1

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()));
}
share|improve this answer

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.