[英]Remove Marker - Google Maps Javascript API v3
Trying out tha Maps API. 试用tha Maps API。 I got stuck at removing markers.
我被困在删除标记上。 Adding Marker works great, but deletion isnt working.
添加标记效果很好,但是删除却不起作用。 Any idea why this would not work?
知道为什么这行不通吗? Thank in advance!
预先感谢!
function placeMarker(event) {
var marker = new google.maps.Marker({
clickable: true,
position: event.latLng,
map: map
});
}
function deleteMarker() {
marker.setMap(null);
marker = null;
}
function initialize() {
var mapOptions = {
zoom: 8,
center: home
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListener(map,'rightclick', placeMarker);
google.maps.event.addListener(marker,'click', deleteMarker);
}
google.maps.event.addDomListener(window, 'load', initialize);
Defining global variables 定义全局变量
var map,
marker,
home = new google.maps.LatLng(46.5, 13.5);
results in error message: 导致错误消息:
Uncaught TypeError: Cannot read property '__e3_' of undefined main.js:15
from line 从线
google.maps.event.addListener(marker, 'click', deleteMarker);
marker
is still undefined
because it is not created and event listener cannot be attached to it. marker
仍未undefined
因为未创建marker
,并且无法将事件侦听器附加到该marker
。 That line has to be moved to placeMarker()
function: 该行必须移至
placeMarker()
函数:
function placeMarker(event) {
marker = new google.maps.Marker({
clickable: true,
position: event.latLng,
map: map
});
google.maps.event.addListener(marker, 'click', deleteMarker);
}
See example at jsbin . 请参阅jsbin上的示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.