获取地标的纬度和经度值 - Google地球

时间:2012-04-13 08:43:51

标签: kml google-earth-plugin

我使用的是Google Earth API,我是新手。我使用kml标记了一个地标,并为此添加了一个点击事件。我想获得这个地标的纬度和经度值。虽然我使用 getLatitude() getLongitude()函数,但值不准确。我想要与kml中定义的完全相同的值。我得到的值因点而异。

有没有办法解决这个问题?

谢谢

Shubhra

1 个答案:

答案 0 :(得分:3)

我为您准备了以下示例。它获取kml文件并将click事件附加到地标。提醒的纬度和经度值与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>
相关问题