我想使用其他按钮而不是图形管理器控件开始在地图上绘制多边形。
是否有可能触发一个事件,其行为与按图形管理器多边形控件一样?
要使用其他控件开始绘制多边形,请执行以下操作:
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);
});
}
/* 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句