简体   繁体   中英

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.

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. 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?


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.

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.

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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM