从多边形获取地理编码信息

时间:2016-07-26 15:02:02

标签: google-maps-api-3 polygon reverse-geocoding

我正在创建一个应用程序,用户可以在谷歌地图上绘制多边形,并获取其坐标并保存它们。好吧,我已经完成了这一切。

但我现在要做的是从该多边形中提取地理编码信息(街道,地址......),我知道如何为给定的Lat / Lng进行反向地理编码,但我的问题是我该怎么做它有一个多边形?

这是我的代码:

<script src="https://maps.googleapis.com/maps/api/js?v=3.20&callback=initMap&libraries=drawing" async
        defer>
</script>
<script>
    var drawingManager;
    var map;


    function initMap() {

        var mapDiv = document.getElementById('map');
        map = new google.maps.Map(mapDiv, {
            center: {lat: 43.598899, lng: 1.433759},
            zoom: 14
        });

        drawingManager = new google.maps.drawing.DrawingManager({
            drawingControl: true,
            drawingControlOptions: {
                position: google.maps.ControlPosition.TOP_CENTER,
                drawingModes: [
                    google.maps.drawing.OverlayType.POLYGON,
                    google.maps.drawing.OverlayType.RECTANGLE
                ]
            }
        });
        drawingManager.setMap(map);

        google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
            if (e.type != google.maps.drawing.OverlayType.MARKER) {
                // Switch back to non-drawing mode after drawing a shape.
                drawingManager.setDrawingMode(null);

                // Add an event listener that selects the newly-drawn shape when the user
                // mouses down on it.
                var newShape = e.overlay;
                newShape.type = e.type;

                var pointsArray = [];

               if (e.type == google.maps.drawing.OverlayType.RECTANGLE) {
                    var rectangle = e.overlay;
                    var bounds = rectangle.getBounds();

                    var NE = bounds.getNorthEast();
                    var SW = bounds.getSouthWest();

                }
                else if (e.type == google.maps.drawing.OverlayType.POLYGON) {
                    var polygon = e.overlay;
                    var coordinatesArray = polygon.getPath();
                    for (var i = 0; i < coordinatesArray.getLength(); i++) {
                        var xy = coordinatesArray.getAt(i);
                        var lat = xy.lat();
                        var lng = xy.lng();
                        pointsArray.push({"lat": lat, "lng": lng});
                    }


                }
            }
        });

    }
</script>

感谢。

0 个答案:

没有答案