我想开始使用不同的按钮而不是绘图管理器控件在地图上绘制多边形。
是否可以触发一个与按下绘图管理器多边形控件一样的事件?
答案 0 :(得分:3)
使用其他控件开始绘制多边形:
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);
});
代码段
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);
});
}
&#13;
/* 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;
}
&#13;
<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" async defer></script>
&#13;