2012-04-13 51 views
2

我正在使用Google地球API,但我對此很陌生。我已經使用kml標記了地標,併爲此添加了一個單擊事件。我想獲取這個地標的經度和緯度值。儘管我使用getLatitude()()getLongitude()函數,但值不準確。我想要的值與在kml中定義的完全相同。我得到的數值因點而異。獲取地標的經度和緯度值 - Google地球

有沒有什麼辦法呢?

感謝

Shubhra

+0

顯示你正在使用的一些代碼,javascript和kml – lifeIsGood 2012-04-13 17:58:28

回答

3

我對你下面的示例。它讀取kml文件並將附加的點擊事件附加到地標。提示的緯度和經度值與kml文件完全相同。希望能幫助到你。

<html> 
<head> 
<title>sample.html</title> 
<script src="http://www.google.com/jsapi?key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script> 
<script type="text/javascript"> 
    var ge; 
    google.load('earth', '1'); 

    function init() { 
    google.earth.createInstance('map3d', initCB, failureCB); 
    } 

    function initCB(instance) { 
    ge = instance; 
    ge.getWindow().setVisibility(true); 

    var href = 'http://code.google.com/' 
       + 'apis/earth/documentation/samples/kml_example.kml'; 

    google.earth.fetchKml(ge, href, function(kmlObject) { 
      if (kmlObject) 
      { 
       ge.getFeatures().appendChild(kmlObject); 
       google.earth.addEventListener(kmlObject, 'click', function(event) { 
       var placemark = event.getTarget(); 
       alert('Latitude :' + placemark.getGeometry().getLatitude() 
        +' Longitude :' + placemark.getGeometry().getLongitude()); 
       }); 
      } 
      if (kmlObject.getAbstractView() !== null) 
       ge.getView().setAbstractView(kmlObject.getAbstractView()); 
    });     
    } 

    function failureCB(errorCode) { 
    } 
    google.setOnLoadCallback(init); 
</script> 

</head> 
<body> 
    <div id="map3d" style="border: 1px solid silver; height: 400px; width: 600px;"> </div> 
</body> 
</html> 
+1

非常感謝你,那對我有效,但仍然存在一些問題。有時它會給出不完全相同的價值。例如:如果在kml經度爲27.19737,則placemark.getGeometry()。getLongitude()返回27.1973799999999995。雖然四捨五入後的值仍然相同,但仍不準確,這對於某些情況並非全部。 – Shubhra 2012-04-17 04:53:59

相關問題