我需要在地图上放置多个标记,然后才能点击它们。然后,应该在我的网站内启动某个JS,为此,我正在使用以下代码:
function initialize() {
geocoder = new google.maps.Geocoder();
var mapCanvas = document.getElementById('map_canvas');
var mapOptions = {
center: new google.maps.LatLng(64.113598, -21.8569031),
zoom: 12,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
function getAddress (address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
return;
}
});
}
getAddress("some address here");
}
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(8);
});
google.maps.event.addDomListener(window, 'load', initialize);
但这给了我Uncaught ReferenceError: marker is not defined
错误。我究竟做错了什么?
您已var marker
在函数内部定义了getAddress
,因此该变量仅在此范围内可用。请getAddress
像这样将变量定义移出:
var marker;
function initialize() {
// ...
function getAddress (address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
return;
}
});
}
getAddress("some address here");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句