gpt4 book ai didi

google-maps-api-3 - 如何在 Google Maps API 中触发开始多边形绘制?

转载 作者:行者123 更新时间:2023-12-02 04:00:10 24 4
gpt4 key购买 nike

我想开始使用不同的按钮而不是绘图管理器控件在 map 上绘制多边形。

是否可以触发一个行为类似于按下绘图管理器多边形控件的事件?

最佳答案

要使用不同的控件开始绘制多边形:

  1. 删除现有的绘图控件:
drawingControl: false,
  • 添加您自己的按钮:
  • <input id="polygonbtn" type="button" value="Draw Polygon"/>
  • 设置点击时的绘图模式:
  • google.maps.event.addDomListener(document.getElementById('polygonbtn'), 'click', function() {
    drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYGON);
    });

    proof of concept fiddle

    代码片段:

    function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
    center: {
    lat: -34.397,
    lng: 150.644
    },
    zoom: 8
    });

    var drawingManager = new google.maps.drawing.DrawingManager({
    drawingControl: false,
    });
    drawingManager.setMap(map);
    google.maps.event.addDomListener(document.getElementById('polygonbtn'), 'click', function() {
    drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYGON);
    });
    google.maps.event.addDomListener(document.getElementById('cancelbtn'), 'click', function() {
    drawingManager.setDrawingMode(null);
    });
    }
    /* Always set the map height explicitly to define the size of the div
    * element that contains the map. */

    #map {
    height: 100%;
    }


    /* Optional: Makes the sample page fill the window. */

    html,
    body {
    height: 100%;
    margin: 0;
    padding: 0;
    }
    <input id="polygonbtn" type="button" value="Draw Polygon" />
    <input id="cancelbtn" type="button" value="Cancel" />
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&callback=initMap&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk" async defer></script>

    关于google-maps-api-3 - 如何在 Google Maps API 中触发开始多边形绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42278517/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com