我想创建一个简单的应用程序,用户可以绘制一个圆圈并调整大小并拖动它 并在最后接收中心位置和距离。 我做了它并且它工作正常但是当拖动或调整圆形时,事件不会触发。
这是我的代码,请注意overlaycomplete
和circlecomplete
有效。
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing"></script>
<div id="map-canvas"></div>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.CIRCLE,
drawingControl: false,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.CIRCLE
]
},
circleOptions: {
fillColor: '#000000',
fillOpacity: 0.6,
strokeWeight: 2,
clickable: false,
editable: true,
zIndex: 1
}
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
var coordinates = (e);
drawingManager.setDrawingMode(null);
});
google.maps.event.addListener(drawingManager, 'circlecomplete', function (e) {
var coordinates = (e);
console.log(coordinates);
console.log('radius', coordinates.getRadius());
console.log(coordinates.getCenter().lat())
console.log(coordinates.getCenter().lng())
});
google.maps.event.addListener(drawingManager, 'center_changed', function (e) {
var coordinates = (e);
console.log(coordinates);
console.log('radius', coordinates.getRadius());
console.log(coordinates.getCenter().lat())
console.log(coordinates.getCenter().lng())
});
google.maps.event.addListener(drawingManager, 'distance_changed', function (e) {
var coordinates = (e);
console.log(coordinates);
console.log('radius', coordinates.getRadius());
console.log(coordinates.getCenter().lat())
console.log(coordinates.getCenter().lng())
});
drawingManager.setMap(map);
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
答案 0 :(得分:1)
似乎center_changed
和distance_changed
不是DrawingManager Class发送的事件
Map class调度了一个center_changed
事件,但是当地图中心发生变化时触发此事件,而不是圆形覆盖的中心。
编辑:(未经测试)
您必须使用radius_changed
,drag
或dragend
google.maps.event.addListener(drawingManager, 'circlecomplete', function (circle) {
console.log('radius at creation', circle.getRadius());
console.log('lat at creation', circle.getCenter().lat())
console.log('lon at creation', circle.getCenter().lng())
google.maps.event.addListener(circle, 'radius_changed', function () {
console.log('radius after change', circle.getRadius());
console.log('lat after radius change', circle.getCenter().lat())
console.log('lon after radius change', circle.getCenter().lng())
});
google.maps.event.addListener(circle, 'dragend', function () {
console.log('radius at dragend', circle.getRadius());
console.log('lat at dragend', circle.getCenter().lat())
console.log('lon at dragend', circle.getCenter().lng())
});
});