Google Api矩形和标记值集

时间:2018-08-23 13:00:39

标签: javascript google-maps javascript-events google-maps-markers

下面的代码正在运行,但是无法在(顶点文本字段)或矩形坐标中都无法获取标记的当前位置。 我也想防止一次添加多个形状!非常感谢您的帮助同伴

var drawingManager;
                var all_overlays = [];
                var selectedShape;
                var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
                var selectedColor;
                var colorButtons = {};
                var circle;
                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-canvas'), {
                        zoom: 10,
                        center: new google.maps.LatLng(<?php echo $_COOKIE['map_default_lat'];?>, <?php echo $_COOKIE['map_default_lng'];?>),
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        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;
                            if (e.type == 'polygon')
                            {
                                overlayClickListener(e.overlay);
                                $('#vertices').val(e.overlay.getPath().getArray());
                            }
                            if (e.type == 'circle')
                            {
                                google.maps.event.addListener(drawingManager, 'circlecomplete', onCircleComplete);
                            }if (e.type == 'polyline')
                            {
                                overlayClickListenerPoly(e.overlay);
                                $('#vertices').val(e.overlay.getPath().getArray());
                            }
                            google.maps.event.addListener(newShape, 'click', function() {
                                setSelection(newShape);
                            });
                            setSelection(newShape);
                        } else
                        {
                            alert(e.latlng); // this fires an error
                        }
                    });
                    function overlayClickListenerPoly(overlay)
                    {
                        google.maps.event.addListener(overlay, "mouseup", function(event) {
                            $('#vertices').val(overlay.getPath().getArray());
                        });
                    }
                    function overlayClickListener(overlay) {
                        google.maps.event.addListener(overlay, "mouseup", function(event) {
                            $('#vertices').val(overlay.getPath().getArray());
                        });
                    }

                    function onCircleComplete(shape) {
                        if (shape == null || (!(shape instanceof google.maps.Circle))) return;

                        if (circle != null) {
                            circle.setMap(null);
                            circle = null;
                        }

                        circle = shape;
                        console.log('radius', circle.getRadius());
                        console.log('lat', circle.getCenter().lat());
                        console.log('lng', circle.getCenter().lng());
                    }
                    // 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);
 #map-canvas {
        height: 100%;
        min-height: 200px;
    }
    /* Optional: Makes the sample page fill the window. */
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    #panel {
        width: 200px;
        font-family: Arial, sans-serif;
        font-size: 13px;
        float: right;
        margin: 10px;
    }

    #color-palette {
        clear: both;
    }

    .color-button {
        width: 14px;
        height: 14px;
        font-size: 0;
        margin: 2px;
        float: left;
        cursor: pointer;
    }

    #delete-button {
        margin-top: 5px;
    }
<script src="http://maps.google.com/maps/api/js?libraries=drawing&sensor=false"></script>
<div id="panel">
                    <div id="color-palette"></div>
                    <div>
                        <button id="delete-button">Delete Selected Shape</button>
                        <button id="delete-all-button">Delete All Shapes</button>
                        <form method="post" accept-charset="utf-8" id="map_form">
                            <input type="text" name="vertices" value="" id="vertices" />
                            <input type="button" name="save" value="Save!" id="save" />
                        </form>
                    </div>
                </div>

            <div id="map-canvas"></div>

我很幸运地做了形状,圆形(它完美记录到控制台),线条,shapecomplete之后,这些值将直接显示在文本框中,如何在同一文本框中获得标记位置值,矩形坐标值?请帮忙 !

谢谢您

0 个答案:

没有答案