gpt4 book ai didi

google-maps - 如何获取谷歌地图多边形顶点绘制/固定事件?

转载 作者:行者123 更新时间:2023-12-01 09:14:32 27 4
gpt4 key购买 nike

我想允许用户在多边形内部绘制多边形。我可以使用 constainsLocation 函数 ( https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation ) 获取纬度/经度但是当用户绘制多边形时我想检测用户绘制的点/顶点是否在多边形内。那么当用户绘制多边形时,是否有任何事件可以检测点击/绘制事件?

	
var map; // Global declaration of the map
var iw = new google.maps.InfoWindow(); // Global declaration of the infowindow
var lat_longs = new Array();
var markers = new Array();
var drawingManager;
function initialize() {
var myLatlng = new google.maps.LatLng(40.9403762, -74.1318096);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [google.maps.drawing.OverlayType.POLYGON]
},
polygonOptions: {
editable: true
}
});
drawingManager.setMap(map);

google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
var newShape = event.overlay;
newShape.type = event.type;
});

google.maps.event.addListener(drawingManager, "overlaycomplete", function(event){
overlayClickListener(event.overlay);
$('#vertices').val(event.overlay.getPath().getArray());
});
}
function overlayClickListener(overlay) {
alert('hello');
google.maps.event.addListener(overlay, "mouseup", function(event){
$('#vertices').val(overlay.getPath().getArray());

});
}
initialize();

$(function(){
$('#save').click(function(){
//iterate polygon vertices?
});
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing&dummy=.js"></script>
<div id="map_canvas" style="width:500px; height:450px;"></div>
<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>

当用户绘制第一个多边形时,用户不应该能够在它之外绘制另一个多边形。有事件吗?

谢谢

最佳答案

您可以使用绘图管理器将多边形之外的 Google map 区域设置为不可编辑。

一旦用户绘制了一个多边形(当您看到用户从他开始的地方到达了相同的点/顶点时)调用这个函数来使谷歌地图的其余区域不可编辑。

$('#showRes').click(function(){
drawingManager.setDrawingMode(null);
drawingManager.setOptions({
polygonOptions: {
paths: pathArr,
strokeWeight: 2,
fillOpacity: 0.2,
editable: false,
fillColor: '#FF0000',
strokeColor: '#FF0000',
geodesic: true,
suppressUndo: true
}
});
}

希望对您有所帮助!!

关于google-maps - 如何获取谷歌地图多边形顶点绘制/固定事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424687/

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