【发布时间】:2016-07-21 00:21:11
【问题描述】:
我正在编写一个 Web 应用程序,它在 Google 地图上围绕一个标记显示一个圆圈。 placeMarker(location, radius) 在 location 处设置一个 Marker 并将一个具有半径的 Circle 绑定到它。我希望脚本在每次调用 placeMarker 时重绘 Circle 和 Marker。当我在控制台中尝试时,它会使用新位置重绘标记,但会保留原始圆半径。 undefined 也会被打印出来。我需要进行哪些更改才能使其正常工作?
var map;
var marker;
function placeMarker(location, radius) {
if (typeof radius === 'undefined') { radius = initialRadius; }
if (marker) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map,
draggable: true
});
}
var circle = new google.maps.Circle({
map: map,
radius: radius,
fillColor: '#AA0000',
});
circle.bindTo('center', marker, 'position');
}
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 1
});
google.maps.event.addListener(map, 'click', function (event) {
placeMarker(event.latLng);
});
google.maps.event.addDomListener(window, "resize", function () {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
}
【问题讨论】:
-
使用发布的代码,我收到一个 javascript 错误:
Uncaught ReferenceError: initialRadius is not defined。如果我解决了这个问题,it works as I expect(以及我认为你想要的方式,除了每次点击地图时圆圈会变暗)。我没有看到任何改变半径的机制。
标签: javascript google-maps google-maps-api-3 google-api google-maps-markers