Search code examples
google-earthgoogle-earth-plugin

Search Placemarks Google Earth API


I know in the Google Earth client you can search for a placemark is there a way to implement this type of feature for the API. We have different placemarks that users would like to search then fly to.

Thanks


Solution

  • Yes, there are lots of ways to search for placemarks, really it would depend on how you have defined or created them.

    For example, are they defined in a KML file, or created via the API? Also, do they have any abstract view defined, or are they simply based on point geometry?

    One of the easiest ways would be to use the various accessors to find the placemarks. For example if the placemark has an ID you can use getElementById() and presuming the placemarks have an abstract view you can use that to look at, or fly to, it.

    var placemark = ge.getElementById('MyPlacemark');
    if (placemark.getAbstractView()) {
       ge.getView().setAbstractView(placemark.getAbstractView());
    }
    

    Or else if you are loading KML by its URL, e.g.

    // loaded via KML
    var placemark = ge.getElementByUrl('http://yoursite.com/foo.kml#MyPlacemark');
    if (placemark.getAbstractView()) {
       ge.getView().setAbstractView(placemark.getAbstractView());
    }
    

    If the placemark doesn't have an abstract view you can still use the accessors to find the correct placemark and then extract the geometry from it to create the look at.

    var placemark = ge.getElementByUrl('http://yoursite.com/foo.kml#MyPlacemark');
    var point = placemark.getGeometry();
    var lat = point.getLatitude();
    var lng = point.getLongitude();
    

    Without more information on how you have defined your placemarks it is pretty hard to be more specific.