Virtual Earth 'nearest' location

Ever wonder what happens when you put your
team from work in a locked room for 24 hours and ‘force’ them to make a site based
on a certain ‘issue’. If you do take a look at
24 heures de Tam Tam
(In Dutch
though). Last Friday we had this ‘event’ for the first time and as you can on the
pictures / movies we had a lot of fun.

It was for this event that I had brush up
my Virtual Earth skills, since as a team we decided to make a small ‘route planner’
based on Virtual Earth. The idea was to create a new map, set up a layer containing
a set of pushpins with the locations of all Snack corners, and based on the current
location of the user show a route to the nearest Snack Corner. Pretty plain and simple
we thought.  It happens to be a bit complicated
though; the Virtual Earth API doesn’t have an option to get nearest pushpin, and getting
the route for each pushpin and then deciding what the nearest location is would probably
fail when there are more than 5 pushpins.

Then the old school math struck me: why not
use Pythagoras do decide what the nearest pushpin is (though it might be not as precise
as getting the Route and determine the exact distance,  but
getting the overall distance it is a great improvement on performance).

So assuming you have a map, containing
a set of pushpins (tip: check the veLayerSpec with GeoRSS), you can loop trough each
pushpin and set the latitude and longitude to an array, and define two vars, one for
the ID of the pushpin and one of the value of the Pushpin ( I used 999).  Then
the next simple function  will be sufficient
(comments inline):  

    function GetNearest(latlon)

latlon should be the location searchin from


        var temp = new String(latlon);

        var mySplitResult = temp.split(",")


        var searchLat = mySplitResult[0];

        var searchLon = mySplitResult[1];


latlon is striped in to two values

pushPinsLat is the lenght of the array that we will check our current lat/lon against

So we loop trough the array with pushpin locations

       for (var i=0, len=pushPinsLat.length; i<len; ++i )



         var ppLat = pushPinsLat[i];

         var ppLon = pushPinsLon[i];


Math.abs returns an absolute value;

         var latcount = Math.abs(ppLat searchLat);

         var loncount = Math.abs(ppLon searchLon);


gets our lat/lon raised to the power of 2

         var temp = Math.pow(latcount, 2);

         var temp2 = Math.pow(loncount, 2);


When we have the lat / lon we can do a square root

the dst will resemble the 'distance',

         var dst = Math.sqrt(temp+temp2);


Then only set the the pushpinVar containing the ID when the current ID is smaller

When were done looping trough the set ppID contains the pushpin ID nearest to current

         if (dst < ppValue)


             ppValue = dst;

             ppID = latlon;





So the math saved the day, since
the actual getting of the route is easy; the API allows you to get a route with map.GetDirections(locations,

Leave a Reply