gpt4 book ai didi

google-maps-api-3 - 计算多边形的面积

转载 作者:行者123 更新时间:2023-12-04 16:24:44 39 4
gpt4 key购买 nike

我需要在我的 map 中获取多边形的面积。我找了一个 new google.maps.geometry.spherical.computeArea 的例子,但我不能让它工作,我不知道为什么。

function dibuV(area){
var i;
var a = new Array();
for(i=0; i<area.length; i++){
var uno = area[i].split(",");
a[i] = new google.maps.LatLng(uno[0],uno[1]);
}
poligon = new google.maps.Polygon({
paths: a,
strokeColor: "#22B14C",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#22B14C",
fillOpacity: 0.35
})
poligon.setMap(map);//until here is ok
var z = new google.maps.geometry.spherical.computeArea(poligon.getPath());
alert(z); //this is not working
}

最佳答案

您的代码给您的错误:google.maps.geometry is undefined
来自 Google Maps v3 API documentation ,几何函数是默认未加载的库的一部分:

The concepts within this document refer to features only available within the google.maps.geometry library. This library is not loaded by default when you load the Maps Javascript API but must be explicitly specified through use of a libraries bootstrap parameter.



为了在您的页面中加载和使用几何函数,您需要通过包含 libraries=geometry 将几何库添加到您的初始 Google API 调用中来通知 Google 您将使用几何库。 :
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>

这将加载几何库和您的 z变量将包含一个对象。

带有工作代码的测试页面:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>
<script type="text/javascript">
var map;
function initialize()
{
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
function test()
{
var arr = new Array()
arr.push('51.5001524,-0.1262362');
arr.push('52.5001524,-1.1262362');
arr.push('53.5001524,-2.1262362');
arr.push('54.5001524,-3.1262362');
dibuV(arr);
}
function dibuV(area)
{
var a = new Array();

for(var i=0; i<area.length; i++)
{
var uno = area[i].split(",");
a[i] = new google.maps.LatLng(uno[0],uno[1]);
}

poligon = new google.maps.Polygon({
paths: a,
strokeColor: "#22B14C",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#22B14C",
fillOpacity: 0.35
})

poligon.setMap(map);//until here is ok
var z = new google.maps.geometry.spherical.computeArea(poligon.getPath());
alert(z); //this is not working
}
</script>
</head>
<body onload="test();">
<div id="map_canvas"></div>
</body>
</html>

关于google-maps-api-3 - 计算多边形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10765667/

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