admin管理员组文章数量:1355721
Im using the following code and trying to figure out how to add a function I can call that will remove the previous marker before adding a new one. the map uses onclick events to add marker where the user clicks... basically i only want one marker on the map at any given time.
i've searched and tried almost everything but must be doing it wrong somehow... please give the code a quick glance and let me know how it would be achieved.
thanks a million!
<script>
function initialize() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(document.getElementById('field_lat').value,document.getElementById('field_lng').value),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
google.maps.event.addListener(map, 'click', function(e) {
// SET MY UI SEARCH TEXT FIELDS TO THE LAT AND LNG
document.getElementById('field_lat').value = e.latLng.lat();
document.getElementById('field_lng').value = e.latLng.lng();
placeMarker(e.latLng, map);
});
}
function placeMarker(position, map) {
//Marker.setMap(null);
var Marker = new google.maps.Marker({
position: position,
map: map
});
map.panTo(position);
$("#listbox ul").empty();
docall();
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
the map is in the body as follows
<div id="map_canvas" style="width: 400px; height: 400px;"></div>
I tried to try to get this or something similar to work but no dice...
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
Im using the following code and trying to figure out how to add a function I can call that will remove the previous marker before adding a new one. the map uses onclick events to add marker where the user clicks... basically i only want one marker on the map at any given time.
i've searched and tried almost everything but must be doing it wrong somehow... please give the code a quick glance and let me know how it would be achieved.
thanks a million!
<script>
function initialize() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(document.getElementById('field_lat').value,document.getElementById('field_lng').value),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
google.maps.event.addListener(map, 'click', function(e) {
// SET MY UI SEARCH TEXT FIELDS TO THE LAT AND LNG
document.getElementById('field_lat').value = e.latLng.lat();
document.getElementById('field_lng').value = e.latLng.lng();
placeMarker(e.latLng, map);
});
}
function placeMarker(position, map) {
//Marker.setMap(null);
var Marker = new google.maps.Marker({
position: position,
map: map
});
map.panTo(position);
$("#listbox ul").empty();
docall();
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
the map is in the body as follows
<div id="map_canvas" style="width: 400px; height: 400px;"></div>
I tried to try to get this or something similar to work but no dice...
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
Share
Improve this question
asked Jan 19, 2013 at 7:27
tamaktamak
1,5912 gold badges22 silver badges43 bronze badges
2
- Why don't you just move the existing marker to the new position? No need to remove and re-create. – Marcelo Commented Jan 19, 2013 at 8:40
- possible duplicate of Google Maps API v3: How to remove all markers? – Touchpad Commented Mar 28, 2014 at 12:55
3 Answers
Reset to default 4Just remove the previous marker if there is one.
var Marker;
function placeMarker(position, map) {
if(typeof marker!= 'undefined')
marker.setMap(null);
var Marker = new google.maps.Marker({
position: position,
map: map
});
map.panTo(position);
$("#listbox ul").empty();
docall();
}
Simply do the following:
I. Declare a global variable:
var markersArray = [];
II. Define a function:
function clearOverlays() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
}
OR
google.maps.Map.prototype.clearOverlays = function() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
}
III. Push markers in the 'markerArray' before calling the following:
markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});
IV. Call the function wherever required.
clearOverlays();
OR
map.clearOverlays();
That's it!!
Hope that will help you.
Source: Google Maps API v3: How to remove all markers?
You can set the marker on first click and then just change the position on subsequent clicks.
var marker;
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
if (marker == null) {
marker = new google.maps.Marker({
position: location,
map: map
});
} else {
marker.setPosition(location);
}
}
本文标签: javascriptGoogle Maps api v3remove marker before adding new one from click eventStack Overflow
版权声明:本文标题:javascript - Google Maps api v3 - remove marker before adding new one from click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743994987a2572824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论