[英]How to get longitude, latitude and altitude of clicked point on a placemark?
I'm using google earth plugin api to create and show placemarks to users. 我正在使用Google Earth插件api创建地标并向用户显示地标。
Exampale code of creating and show placemark: 创建并显示地标的示例代码:
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);
}
Now i need to know whether user has clicked the placemark (i do it with event listener) and what the longitude, latitude and altitude of the point in the placemark he clicked. 现在,我需要知道用户是否单击了地标(我使用事件监听器来完成),以及他单击的地标中该点的经度,纬度和高度。
The problem is that google earth returns the values of the "click" event on the earth surface instead of on the clicked placemark. 问题是Google Earth返回地球表面而不是被点击的地标上的“ click”事件的值。 In some cases the polygon is not lay on the earth (as in the exampale code) and the values are not appropriate. 在某些情况下,多边形不在地面上(例如示例代码),并且值不适当。
I have tried to find a way to get the position of the balloon that opened when placemark is clicked, but with no success. 我试图找到一种方法来获取单击地标时打开的气球的位置,但没有成功。
Is there a way to get that values? 有没有办法获得这些价值?
Edit: 编辑:
I'm using somthing similar to the following simplified code : 我正在使用类似于以下简化代码的东西:
<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>
You don't show how you attach your event handler, but it sounds like you have attached it to the GEGlobe or GEWindow object rather than the KmlPlacemarks. 您没有显示如何附加事件处理程序,但是听起来好像您已将其附加到GEGlobe或GEWindow对象而不是KmlPlacemarks。
To attach an event listener to the specific placemark you would do something like so. 要将事件侦听器附加到特定地标,您可以执行类似的操作。
google.earth.addEventListener(polygonPlacemark , 'click', doSomething);
function doSomething(event) {
var lat = event.getTarget().getGeometry().getLatitude();
var lng = event.getTarget().getGeometry().getLongitude();
// do something with lat, lng
}
If you want to create a 'catch all' event handler that handles click events on all placemarks you can do it using the GEGlobe
object then using conditional logic to screen for placemark objects. 如果要创建一个“全部捕获”事件处理程序来处理所有地标上的单击事件,则可以使用GEGlobe
对象,然后使用条件逻辑来筛选地标对象,以完成此操作。
google.earth.addEventListener(ge.getGlobe(), 'click', doSomething);
function doSomething(event) {
if (event.getTarget().getType() == 'KmlPlacemark') { //check the event target
var lat = event.getTarget().getGeometry().getLatitude();
var lng = event.getTarget().getGeometry().getLongitude();
// do something with lat, lng
}
}
Edit: 编辑:
Based on your comment, it sounds like you want to attach the click event to the polygon
itself, then extract the position that event took place. 根据您的评论,听起来您想将click事件附加到polygon
本身,然后提取事件发生的位置。 If so this should work. 如果是这样,这应该起作用。
google.earth.addEventListener(polygon , 'click', doSomething);
function doSomething(event) {
var lat = event.getLatitude();
var lng = event.getLongitude();
var alt = event.getaltitude();
// do something with lat, lng, alt
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.