2012-05-26 120 views
5

我需要獲取地圖中多邊形的面積。我尋找一個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 
} 

回答

15

你的代碼是給你的錯誤:google.maps.geometry is undefined

Google Maps v3 API documentation,幾何函數是默認不加載的庫的一部分:

內這個概念文檔請參閱google.maps.geometry庫中的僅適用於 的功能。當您加載Maps JavaScript API時,此庫不會被加載,默認爲 ,但必須通過使用庫引導參數指定 。

爲了加載並使用你的頁面內的幾何funcitons,你需要通過包括libraries=geometry通知谷歌,你將要使用的幾何庫,將其添加到您最初的谷歌API調用:

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

然後這將加載幾何庫,並且您的變量將包含一個對象。有工作代碼

測試頁:

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

你怎麼知道他得到那個錯誤,我想知道?就我而言,在繪圖庫已經加載的情況下,添加您建議的腳本標籤會導致該過程失敗。圖形庫中是否包含幾何圖形?適用於我。 (就像萊昂納多說的那樣,在靜態方法面前失去了'新') – fortboise

+0

謝謝,當我遇到這個時,我花了很多時間撓撓腦袋。在我的情況下,它是在庫的這部分被加載之前觸發jquery加載事件。 –

+1

如果您需要多個庫,只需用逗號分隔它們:'?libraries = geometry,visualization' ..etc。 – elrobis

6

這是行不通的,因爲使用的是「新」的使用方法computeArea。 在沒有「新建」的情況下使用「google.maps.geometry.spherical.computeArea」。 例如:

var z = google.maps.geometry.spherical.computeArea(myPolyline.getPath().getArray()); 
alert(z); 

這將工作。