Thursday, 15 August 2013

javascript - Google Maps API V3 Indicate multiple markers in same location by different icon -



javascript - Google Maps API V3 Indicate multiple markers in same location by different icon -

javascript noob here, i'm working through code examples , have page working displays multiple markers. have array of locations , have multiple markers same location. top 1 displays, fine, want indicate there more markers hidden changing icon bit works:

for (i = 0; < locations.length; i++) { marker = new gm.marker({ position: new gm.latlng(locations[i][1], locations[i][2]), map: map, icon: markerimg, }); }

i don't seem able add together conditional code "icon: markerimg" line alter different icon if coords match previous one.

is there way have conditional declarations (if thats right terminology) thanks.

if need check previous marker, maintain reference it:

var lastmarker = null; (i = 0; < locations.length; i++) { var marker = new gm.marker({ position: new gm.latlng(locations[i][1], locations[i][2]), map: map, icon: markerimg, }); if (!!lastmarker && !!lastmarker.getposition && (marker.getposition().equals(lastmarker.getposition()))) { marker.seticon(markerimg2); } lastmarker = marker; }

fiddle

note: careful google.maps.latlng.equals, coordinates have exactly same. might want pick threshold (say 0.1 meter) , consider markers closer "at same location".

javascript google-maps-api-3

No comments:

Post a Comment