Googlemaps-删除以前的标记 [英] Googlemaps - removing previous marker
问题描述
关于如何清除googlemaps上的标记的一些建议之后,我有一张地图,我希望仅显示一个标记(基本上是最后单击的标记).我希望用户能够改变主意并多次单击,但以前的单击等的映射不会令人困惑.
After some advice on how to clear markers on googlemaps, I have a map which I would like to have only one marker showing (basically the last marker clicked). I would like the user to be able to change thier mind and click multiple times, but not have confusing map of previous clicks etc.
我已经尝试过map.clearOverlay();功能,但似乎可以永久清除整个地图.
I have tried the map.clearOverlay(); function, but it seems to permantly clear the whole map.
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("googlemap"));
map.setCenter(new GLatLng(50.401515,-4.866943), 8);
GEvent.addListener(map,"click", function(overlay,latlng) {
if (latlng) {
var myHtml = "" + latlng ;
split=myHtml.indexOf(",");
x=Math.round(myHtml.slice(1,split)*1000000)/1000000;
y=Math.round(myHtml.slice(split+1,myHtml.length-1)*1000000)/1000000;
document.collector.latitude.value=x;
document.collector.longitude.value=y;
lat="<br />Latitude: " + x;
lon="<br />Longitude: " + y;
latlon=""+lat+lon;
//map.openInfoWindow(latlng, latlon);
map.clearOverlay();
map.addOverlay(new GMarker(latlng));
}
});
map.addControl(new GLargeMapControl3D());
map.addControl(new GMapTypeControl());
}
}
推荐答案
未经测试,但应做您想做的-请注意GMarker
的setLatLng
方法是在API版本2.88中引入的:
Untested, but should do what you want - note that the setLatLng
method of GMarker
was introduced in API version 2.88:
function initialize() {
if (GBrowserIsCompatible()) {
var marker;
function showNewMarker(latlng) {
marker = new GMarker(latlng);
map.addOverlay(marker);
showMarker = updateExistingMarker;
}
function updateExistingMarker(latlng) {
marker.setLatLng(latlng);
}
var showMarker = showNewMarker;
var map = new GMap2(document.getElementById("googlemap"));
map.setCenter(new GLatLng(50.401515,-4.866943), 8);
GEvent.addListener(map,"click", function(overlay,latlng) {
if (latlng) {
var myHtml = "" + latlng ;
split=myHtml.indexOf(",");
x=Math.round(myHtml.slice(1,split)*1000000)/1000000;
y=Math.round(myHtml.slice(split+1,myHtml.length-1)*1000000)/1000000;
document.collector.latitude.value=x;
document.collector.longitude.value=y;
lat="<br />Latitude: " + x;
lon="<br />Longitude: " + y;
latlon=""+lat+lon;
//map.openInfoWindow(latlng, latlon);
map.clearOverlay();
showMarker(latlng);
}
});
map.addControl(new GLargeMapControl3D());
map.addControl(new GMapTypeControl());
}
}
它通过使用包含对函数的引用的变量showMarker
来工作.首先指向函数showNewMarker
,该函数创建一个标记,将其添加到地图,然后更改showMarker
指向函数updateExistingMarker
,该函数仅使用setLatLng
将标记移动到新的位置.这意味着,在随后的点击中,现有标记将被移动到该点击的位置.
It works by using a variable, showMarker
, containing a reference to a function. This starts out pointing to a function, showNewMarker
, which creates a marker, adds it to the map, and then changes showMarker
to point to the function updateExistingMarker
, which simply uses setLatLng
to move the marker to a new position. This means that, on subsequent clicks, the existing marker will be moved to the location of the click.
这篇关于Googlemaps-删除以前的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!