计算多边形的面积 [英] Calculate the area of a polygon

查看:115
本文介绍了计算多边形的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的地图中获取多边形的区域。我寻找了一个新的google.maps.geometry.spherical.computeArea 的例子,但是我无法完成它,我不知道为什么。

I need to get the area of a polygon in my map. I looked for an example of new google.maps.geometry.spherical.computeArea, but I can't make it work, and I don't know why.

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未定义

The error that your code is giving you: google.maps.geometry is undefined

Google Maps v3 API文档,几何函数是默认情况下未加载的库的一部分:

From the Google Maps v3 API documentation, the geometry functions are part of a library that is not loaded by default:


本文档中的概念引用了google.maps.geometry库中仅提供
的功能。当您加载地图JavaScript API时,此库不会被
默认加载,但必须通过使用库引导参数来明确指定

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.

为了加载和使用页面中的几何函数,您需要通知Google您将使用几何库,方法是将它添加到初始Google API调用中,方法是将 libraries = geometry

In order to load and use the geometry funcitons within your page, you need to inform Google that you will be using the geometry library, by adding it to your initial Google API call by including libraries=geometry:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>

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

This will then load the geometry library and your z variable will contain an object.

带有工作代码的测试页:

Test page with working code:

<!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>

这篇关于计算多边形的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆