我正在使用谷歌地球插件 api 来创建并向用户显示地标。
创建和显示地标的示例代码:
function createPlacemark(){
var polygonPlacemark = ge.createPlacemark('Polygon');
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
polygonPlacemark.setGeometry(polygon);
var outer = ge.createLinearRing('');
polygon.setOuterBoundary(outer);
var coords = outer.getCoordinates();
coords.pushLatLngAlt(35.3,33.3544921875,1200);
coords.pushLatLngAlt(35.3,33.3544921875,1200);
coords.pushLatLngAlt(35.3,33.3544921875,1000);
coords.pushLatLngAlt(35.3,33.3544921875,1000);
ge.getFeatures().appendChild(polygonPlacemark);
}
现在我需要知道用户是否单击了地标(我使用事件侦听器进行)以及他单击的地标中的点的经度、纬度和高度。
问题是谷歌地球在地球表面而不是在点击的地标上返回“点击”事件的值。在某些情况下,多边形不位于地球上(如示例代码中所示),并且值不合适。
我试图找到一种方法来获取单击地标时打开的气球的位置,但没有成功。
有没有办法获得该值?
编辑:
我正在使用类似于以下简化代码的东西:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></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);
}
function failureCB(errorCode) {
}
google.setOnLoadCallback(init);
function createPlacemark(){
var polygonPlacemark = ge.createPlacemark('Polygon');
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
polygonPlacemark.setGeometry(polygon);
var outer = ge.createLinearRing('');
polygon.setOuterBoundary(outer);
polygonPlacemark.setDescription('test');
var coords = outer.getCoordinates();
coords.pushLatLngAlt(35.3,33.3544921875,1200);
coords.pushLatLngAlt(35.35,33.3544921875,1200);
coords.pushLatLngAlt(35.35,33.3544921875,1000);
coords.pushLatLngAlt(35.3,33.3544921875,1000);
ge.getFeatures().appendChild(polygonPlacemark);
lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
lookAt.setLatitude(35.33);
lookAt.setLongitude(33.3544921875);
lookAt.setRange(4500);
lookAt.setTilt(45);
lookAt.setHeading(90);
// Update the view in Google Earth
ge.getView().setAbstractView(lookAt);
google.earth.addEventListener(polygonPlacemark , 'click', doEvent);
}
function doEvent(event) {
document.getElementById("alt").value=event.getAltitude();
document.getElementById("lon").value=event.getLongitude();
document.getElementById("lat").value=event.getLatitude();
}
</script>
</head>
<body>
<div id="map3d" style="height: 820px; width: 1680px;"></div>
<button id="bCreatePlacemark" type="button" onclick="createPlacemark()">Create Placemark</button><br>
<input id="lon" /><br>
<input id="lat" /><br>
<input id="alt" />
</body>
</html>