3

我有下一个获取多边形坐标的代码,但我想在画圆时获取中心坐标。

有谁知道如何实现这一目标?

最好的问候,并希望有人对此有所了解。

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);

}
4

2 回答 2

4

在 coord_listener 之后添加此代码:

google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
  alert(circle.getCenter());
});
于 2012-12-20T18:24:48.217 回答
0

我遇到了同样的问题,我不得不使用:

const lat = circle.overlay.getCenter().lat()
const lng = circle.overlay.getCenter().lng()
于 2021-03-22T14:34:29.833 回答