【问题标题】:Get Latitude and Longitude values of placemark - Google Earth获取地标的纬度和经度值 - Google 地球
【发布时间】:2013-05-02 05:48:09
【问题描述】:

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

有什么办法吗?

谢谢

舒布拉

【问题讨论】:

  • 显示您正在使用的一些代码,包括 javascript 和 kml

标签: kml google-earth-plugin


【解决方案1】:

我为您准备了以下示例。它获取一个 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>

【讨论】:

  • 非常感谢,这对我有用,但仍然存在一些问题。有时它给出的价值并不完全相同。例如:如果在 kml 中的经度是 27.19737 ,则 placemark.getGeometry().getLongitude() 返回 27.1973799999999995。虽然四舍五入后的值相同,但仍然不准确,这在某些情况下并非全部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
相关资源
最近更新 更多