简体   繁体   English

如何获得地标点击点的经度,纬度和高度?

[英]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.

相关问题 访问地标的Point信息以修改高度 - Accessor to the Point's information of a placemark to modify the altitude 如何将 x、y、z 转换为 Cesium 中的经度、纬度、高度? - How to convert x, y, z to longitude, latitude, altitude in Cesium? 如何获取包含给定纬度/经度点的Leaflet GeoJSON功能 - How to get Leaflet GeoJSON feature containing a given latitude/longitude point 如何从经纬度点获取城市名称? - How can I get city name from a latitude and longitude point? 如何获取yandex地图上点击的地标的ID? - How to get id of clicked placemark on yandex maps? 如何计算由纬度和经度和海拔高度指定的两点(垂直)之间的距离? - How do I calculate the distance between two points (vertically) specified by latitude and longitude and Altitude? 如何获取svg地图起点的纬度和经度坐标(x = 0,y = 0)? - How to get latitude and longitude coordinates of a svg map's starting point(x=0,y=0)? 通过单击Google Maps HTML获取特定点的纬度和经度 - Get latitude and longitude of a certain point by a click in Google Maps HTML 从经纬度获取高度(HERE-API) - Getting altitude from latitude and longitude (HERE-API) 拖动标记时获取起点和终点的纬度和经度 - get latitude and longitude of starting and ending point when a marker is dragged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM