GE Plugin and dynamic URLs.

Recently a question was raised on the GE boards about how to use a dynamic URL to goto to a specific placemark within a KMZ when you load your GE enabled site.

While there are alternatives to doing that - like giving the user options to click within your site - going to a (changeable) default location on load can be useful.

Here’s an example of how to do it.

http://www.thekmz.co.uk/GEPlugin/examples/pminurl/pminurl.htm

In this I have a KML file with 3 placemarks in it - London, Paris and
Dublin (all of which have a LookAt tag)

If you just open the web page it will load the KML but not set the
view to anything.

If you open the web page by appending one of the cities in the URL it
will load the KML and then fly to the relevant city (using the
aformentioned LookAt). ie
http://www.thekmz.co.uk/GEPlugin/examples/pminurl/pminurl.htm?london

will go to London. (I convert to lowercase so case sensitivity doesn’t
come in to play).

Obviously you’ll need to amend this for your own particular
circumstances but the 2 core parts are:-

1. Parse the URL

For this I use…

var urlquery = location.href.split(”?”);

if(urlquery[1]) {
var urlterms = urlquery[1].split(”,”);
curr_pm = urlterms[0];
}

…so the pm I’m later looking for in my KML (curr_pm) is the first
item after the ?. (It’s set so I can pass more options through but for
this only use the first item in the urlterms array).

2. Find the Placemark

For this I used Roman’s kmldomwalk.js
http://code.google.com/p/earth-api-samples/source/browse/trunk/lib/kmldomwalk.js?r=54
(although you can do it whichever way you want) with which I walk
through the KML until I find a Placemark with the same name as my
curr_pm. When, and if, I find it I extract the LookAt from it and fly
there.

This entry was posted in Tips and Snippets and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*