如何从谷歌地图api中找到坐标,以便在地图中显示数据库中的标记

时间:2012-11-01 12:59:04

标签: api

我有一个搜索页面,其中我在数据库中显示属性作为谷歌地图中的标记。现在我想要修改此搜索页面,以便任何用户都可以在Google地图中绘制圆形或多边形,并且只会在所选区域(由用户自己绘制)中从我们的数据库中获取属性。到目前为止,我正在附加我的代码。

图像中的圆圈显示所选区域,结果必须显示所选区域中可用的属性。我的表包含2个不同的字段,用于每个属性的纬度和经度。

Javascript代码:

  <script type="text/javascript">
    var drawingManager;
    var all_overlays = [];
    var selectedShape;
    var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
    var selectedColor;
    var colorButtons = {};

    function clearSelection() {
        if (selectedShape) {
            selectedShape.setEditable(false);
            selectedShape = null;
        }
    }

    function setSelection(shape) {
        clearSelection();
        selectedShape = shape;
        shape.setEditable(true);
        selectColor(shape.get('fillColor') || shape.get('strokeColor'));
    }

    function deleteSelectedShape() {
        if (selectedShape) {
            selectedShape.setMap(null);
        }
    }

    function deleteAllShape() {
        for (var i = 0; i < all_overlays.length; i++) {
            all_overlays[i].overlay.setMap(null);
        }
        all_overlays = [];
    }

    function selectColor(color) {
        selectedColor = color;
        for (var i = 0; i < colors.length; ++i) {
            var currColor = colors[i];
            colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
        }

        // Retrieves the current options from the drawing manager and replaces the
        // stroke or fill color as appropriate.
        var polylineOptions = drawingManager.get('polylineOptions');
        polylineOptions.strokeColor = color;
        drawingManager.set('polylineOptions', polylineOptions);

        var rectangleOptions = drawingManager.get('rectangleOptions');
        rectangleOptions.fillColor = color;
        drawingManager.set('rectangleOptions', rectangleOptions);

        var circleOptions = drawingManager.get('circleOptions');
        circleOptions.fillColor = color;
        drawingManager.set('circleOptions', circleOptions);

        var polygonOptions = drawingManager.get('polygonOptions');
        polygonOptions.fillColor = color;
        drawingManager.set('polygonOptions', polygonOptions);
    }

    function setSelectedShapeColor(color) {
        if (selectedShape) {
            if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
                selectedShape.set('strokeColor', color);
            } else {
                selectedShape.set('fillColor', color);
            }
        }
    }

    function makeColorButton(color) {
        var button = document.createElement('span');
        button.className = 'color-button';
        button.style.backgroundColor = color;
        google.maps.event.addDomListener(button, 'click', function () {
            selectColor(color);
            setSelectedShapeColor(color);
        });

        return button;
    }

    function buildColorPalette() {
        var colorPalette = document.getElementById('color-palette');
        for (var i = 0; i < colors.length; ++i) {
            var currColor = colors[i];
            var colorButton = makeColorButton(currColor);
            colorPalette.appendChild(colorButton);
            colorButtons[currColor] = colorButton;
        }
        selectColor(colors[0]);
    }

    function initialize() {
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 6,
            center: new google.maps.LatLng(22, 77),
            mapTypeId: google.maps.MapTypeId.HYBRID,
            disableDefaultUI: true,
            zoomControl: true
        });

        var polyOptions = {
            strokeWeight: 0,
            fillOpacity: 0.45,
            editable: true
        };
        // Creates a drawing manager attached to the map that allows the user to draw
        // markers, lines, and shapes.
        drawingManager = new google.maps.drawing.DrawingManager({
            drawingMode: google.maps.drawing.OverlayType.POLYGON,
            markerOptions: {
                draggable: true
            },
            polylineOptions: {
                editable: true
            },
            rectangleOptions: polyOptions,
            circleOptions: polyOptions,
            polygonOptions: polyOptions,
            map: map
        });

        google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
            all_overlays.push(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;
                google.maps.event.addListener(newShape, 'click', function () {
                    setSelection(newShape);
                });
                setSelection(newShape);
            }
        });

        // Clear the current selection when the drawing mode is changed, or when the
        // map is clicked.
        google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
        google.maps.event.addListener(map, 'click', clearSelection);
        google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
        google.maps.event.addDomListener(document.getElementById('delete-all-button'), 'click', deleteAllShape);

        buildColorPalette();
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>        

上面的代码包含一些事件来处理搜索页面中的用户交互以绘制cirle,polygone到所选区域中的搜索属性。

图像中的显示属性按钮必须在所选区域中显示属性标记。

如何获得圆形或多边形的选定区域的纬度和经度?
我如何在数据库中查询该范围的坐标?

提前感谢你的帮助...

1 个答案:

答案 0 :(得分:0)

我认为您发布此问题后已找到解决方案。 但是,如果其他人需要答案,那么您可以获得绘图多边形的lat / lng:

google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {
    var polygonBounds = polygon.getPath();
}

多边形的每个顶点的边界现在都在polygonBounds中,只需迭代它并使用lat / lng执行您想要的操作

对于圆形,您只需要中心的lat / lng及其半径

google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
        var radius = circle.getRadius();
        var center = circle.getCenter();
}

希望它可以帮助某人

PS:我正在使用GoogleMaps v3.14,我从不测试其他版本。