获取Polygon所有点的数组 - Google Maps Drawing Tool API-3

时间:2015-06-05 15:15:43

标签: javascript google-maps google-maps-api-3 google-polyline

我使用谷歌绘图工具在谷歌地图上绘制多边形/矩形,现在我需要显示落在绘制多边形内的标记,为此我使用的是geometry.poly.containsLocation方法,它取一个点(latLng)和一个多边形点阵列。如果我使用maps.polygon我可以通过poly.getPath()得到多边形点,但因为我使用Google Drawing tools使用maps.drawing.DrawingManager / google.maps.drawing.OverlayType.POLYGON所以不知道如何找到点在这里。谢谢

  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/car-icon.png'
  },
  circleOptions: {
    fillColor: '#ffff00',
    fillOpacity: 1,
    strokeWeight: 5,
    clickable: false,
    editable: true,
    zIndex: 1
  },
  polygonOptions: {
    fillColor: '#BCDCF9',
    fillOpacity: 0.5,
    strokeWeight: 2,
    strokeColor:'#57ACF9',
    clickable: false,
    editable: false,
    zIndex: 1
  }
});
console.log(drawingManager)
drawingManager.setMap(map)

3 个答案:

答案 0 :(得分:12)

google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
    // assuming you want the points in a div with id="info"
    document.getElementById('info').innerHTML += "polygon points:" + "<br>";
    for (var i = 0; i < polygon.getPath().getLength(); i++) {
        document.getElementById('info').innerHTML += polygon.getPath().getAt(i).toUrlValue(6) + "<br>";
    }
});

proof of concept fiddle

代码段

&#13;
&#13;
var geocoder;
var map;
var polygonArray = [];

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  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
      ]
    },
    /* not useful on jsfiddle
    markerOptions: {
      icon: 'images/car-icon.png'
    }, */
    circleOptions: {
      fillColor: '#ffff00',
      fillOpacity: 1,
      strokeWeight: 5,
      clickable: false,
      editable: true,
      zIndex: 1
    },
    polygonOptions: {
      fillColor: '#BCDCF9',
      fillOpacity: 0.5,
      strokeWeight: 2,
      strokeColor: '#57ACF9',
      clickable: false,
      editable: false,
      zIndex: 1
    }
  });
  console.log(drawingManager)
  drawingManager.setMap(map)

  google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
    document.getElementById('info').innerHTML += "polygon points:" + "<br>";
    for (var i = 0; i < polygon.getPath().getLength(); i++) {
      document.getElementById('info').innerHTML += polygon.getPath().getAt(i).toUrlValue(6) + "<br>";
    }
    polygonArray.push(polygon);
  });

}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,drawing"></script>
<div id="map_canvas" style=" border: 2px solid #3872ac;"></div>
<div id="info"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:5)

geocodezip proposes略有不同的解决方案,但使用相同的API调用:

google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
    var path = polygon.getPath()
    var coordinates = [];

for (var i = 0 ; i < path.length ; i++) {
      coordinates.push({
        lat: path.getAt(i).lat(),
        lng: path.getAt(i).lng()
      });
    }
    console.log(coordinates);
});

答案 2 :(得分:0)

当用户完成绘制内容时,DrawingManager似乎会触发事件,事件处理程序将收到正常的google.maps.Polygongoogle.maps.Rectangle,等等。我会试试。

Here's a documentation link.