javascript - Only want 2 markers on Google Map. Any previous are deleted -
i have user click once, lat/lngs saved inputs. (this have). can click again , next lat/lngs saved second set of inputs. if click again, first marker erased , new marker location saved. once user happy, click save , 2 sets of lat lngs saved in database.
i'm far enough along catch click , place in text boxes save. not sure how second marker , erase 1st if 3rd, , on.
//map clicked google.maps.event.addlistener(map, 'click', function (event) { marker.setposition(event.latlng); map.panto(event.latlng); var clickposition = event.latlng; //map clicked google.maps.event.addlistener(map, 'click', function (event) { marker.setposition(event.latlng); map.panto(event.latlng); var clickposition = event.latlng;
to visualize simple user case: open page embedded google map. click @ point way river. click on point way down same river. lets change mind, move points. 1 @ time, or one.
edit:
ok, able fill 4 text inputs 2 different sets of lat/lngs click along, except previous markers still on map. how did it, using previousmarker global variable. not sure how remove previous unused markers.
google.maps.event.addlistener(map, 'click', function (event) { marker.setposition(event.latlng); map.panto(event.latlng); var clickposition = event.latlng; document.getelementbyid('lat1').value = clickposition.lat().tofixed(6); document.getelementbyid('lng1').value = clickposition.lng().tofixed(6); if (previousmarker) { document.getelementbyid('lat2').value = previousmarker.getposition().lat(); document.getelementbyid('lng2').value = previousmarker.getposition().lng(); } previousmarker = new google.maps.marker({ map: map, position: new google.maps.latlng(clickposition.lat().tofixed(6), clickposition.lng().tofixed(6)) });
a way store markers create in array. can iterate on them, remove them, move them other arrays, , overall have more control on them.something like:
var allmarkers = []; var newmarker = new google.maps.marker({ map: map, position: new google.maps.latlng(clickposition.lat().tofixed(6), clickposition.lng().tofixed(6)) }); allmarkers.push(newmarker); var newmarker2 = new google.maps.marker({ map: map, position: new google.maps.latlng(clickposition.lat().tofixed(6), clickposition.lng().tofixed(6)) }); allmarkers.push(newmarker2); google.maps.event.addlistener(map, 'click', function (event) { allmarkers.foreach(function(element,index,array) { // move element.setposition(event.latlng); }); // or remove allmarkers[0].setmap(null); });
hope helps overall in storing , accessing markers again.
Comments
Post a Comment