我有下一个获取多边形坐标的代码,但我想画一个圆圈时得到中心坐标。
有谁知道如何实现这个目标?
致以最诚挚的问候,并希望有人了解它。
function draw() {
if (drawingManager <= 0) then
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.MARKER,
google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.POLYLINE,
google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {
icon: 'images/beachflag.png'
},
polygonOptions: {
fillColor: '#1E90FF',
fillOpacity: 0.4,
strokeWeight: 2,
clickable: true,
editable: true,
zIndex: 1
}
});
var coord_listener = google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
var coordinates = (polygon.getPath().getArray());
alert(coordinates);
PassPolygonToField(coordinates);
window.poly = polygon;
});
drawingManager.setMap(map);
}
function PassPolygonToField(coordinates) {
//here I pass the coords to the form field and then I insert the form into a mysql database.
document.getElementById('myField').value = (coordinates);
}
答案 0 :(得分:4)
在coord_listener之后添加此代码:
google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
alert(circle.getCenter());
});
答案 1 :(得分:0)
我遇到了同样的问题,我不得不使用:
const lat = circle.overlay.getCenter().lat()
const lng = circle.overlay.getCenter().lng()