我正在使用 Google Earth API,但我是新手。我使用 kml 标记了一个地标,并向其添加了一个单击事件。我想获取该地标的纬度和经度值。虽然我使用 getLatitude() 和 getLongitude() 函数,但值不准确。我想要与 kml 中定义的值完全相同的值。我得到的值因点而异。
有什么办法可以做到这一点吗?
谢谢
舒布拉
最佳答案
我有以下示例给您。它获取 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 - 获取地标的纬度和经度值 - Google 地球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137786/