数组包含Google地图drawingManager中多边形的经度和纬度 [英] Array contains latitude and longitude of a polygon in Google maps drawingManager
问题描述
这是在Google地图上添加绘图管理器以便用户绘制多边形,圆形,矩形等等的最简单的方法。
code:
< html>
< head>
< script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=true&libraries=drawing>< / script>
< script>
函数initialize()
{
var map = new google.maps.Map(document.getElementById('map'),{zoom:12,center:new google.maps.LatLng( 32.344,51.048)});
var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(map);
}
google.maps.event.addDomListener(window,'load',initialize);
< / script>
< / head>
< body>
< div style =width:1024px; height:768px; ID = 地图 >< / DIV>
< / body>
< / html>
我的问题:似乎是你用这个工具绘制了一个多边形。如何读取或创建包含该多边形的经度和纬度的数组?
编辑:另外,也许用户在创建后编辑多边形,所以应该可以在编辑后阅读这个数组,我认为我们不能通过点击地图来使用e参数。
创建一个包含多边形的全局数组
var polygons = [];
然后,将数组填充到 polygoncomplete
事件:
$ b $ pre $ google.maps.event.addDomListener(drawingManager,'polygoncomplete',function(polygon){
polygons。推(多边形);
为了让用户可以在创建后编辑polgons,在 polygonComplete
事件结束时调用 setEditable
polygon.setEditable(true);
如果您需要阅读所有多边形顶点的lat / lng,你可以使用下面这段代码:
var polygonBounds = polygon.getPath() ;
var coordinates = [];
for(var i = 0; i< polygonBounds.length; i ++)
{
coordinates.push(polygonBounds。 getAt(i).lat(),polygonBounds.getAt(i).lng());
}
This is the most simplest way to add a drawing manager on the Google maps to draw polygon, circle, rectangle and etc by user.
code:
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=drawing"></script>
<script>
function initialize()
{
var map = new google.maps.Map(document.getElementById('map'), {zoom: 12, center: new google.maps.LatLng(32.344, 51.048)});
var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div style="width:1024px; height:768px;" id="map"></div>
</body>
</html>
My question: Seems that you draw a polygon by this tool. How do you read or create the array that contains latitude and longitude of this polygon?
Edited: And something else, maybe users edit the polygon after creation so it should be possible to read this array after edition too and I think we can't use "e" parameter by click on the map.
Create a global array which will contains your polygons
var polygons = [];
Then, fill your array in polygoncomplete
event :
google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {
polygons.push(polygon);
In order to let the users the possibility to edit polgons after creation, you have to call setEditable
at the end of your polygonComplete
event
polygon.setEditable(true);
if you need to read all the lat/lng of polygon's vertices, you can use this sample of code :
var polygonBounds = polygon.getPath();
var coordinates = [];
for(var i = 0 ; i < polygonBounds.length ; i++)
{
coordinates.push(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng());
}
这篇关于数组包含Google地图drawingManager中多边形的经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!