【发布时间】:2016-08-02 03:18:49
【问题描述】:
我在屏幕中间有带有标记的地图。 我还有一个带有“允许”区域边界的圆圈,供用户在其中拖动地图的中心。 如果用户将地图拖到此圈外,我需要显示错误消息。 你能告诉我我在下面的代码中做错了什么吗?
我在这一行出现错误:if (_latLngBounds.contains(event.latLng)) 因为event 没有latLng 参数。我不知道如何正确获取当前位置的 lat 和 lng。
var map;
function initialize() {
var _latitudeLongitude = new window.google.maps.LatLng(51.805616, -0.192647);
var circle = new window.google.maps.Circle({
center: _latitudeLongitude,
radius: 50000
});
var _latLngBounds = circle.getBounds();
var locations = [
['WELWYN GARDEN CITY ', 51.805616, -0.192647, 2],
['STANMORE ', 51.603199, -0.296803, 1]
];
map = new google.maps.Map(document.getElementById('map_canvas'), {
navigationControl: true,
scrollwheel: false,
scaleControl: false,
draggable: true,
zoom: 10,
center: new google.maps.LatLng(51.75339, -0.210114),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
zIndex: 10
});
window.google.maps.event.addListener(map , 'drag', function (event) {
marker.setPosition( map.getCenter() );
if (_latLngBounds.contains(event.latLng)) {
// everything is fine
} else {
alert('outside of the boundaries');
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
【问题讨论】:
标签: google-maps google-maps-api-3