Find Location Nearest to You

Currently the default behavior of the VEMap.Find function is to center the map over the matching location that is the most popular. This is great most of the time but sometimes it would be nice to be able to search for a location that is closest to where the map currently is. By default the top 10 most popular locations (VEPlace objects) are returned in the find callback. By modifying the properties in the find call up to 20 locations can be return. By setting another property you can prevent the Find method from centering over the most popular match automatically. By calculating the distance from the center of the map to each returned location in the Find callback we can determine the closest (distance) location that matches the users search. The distance from the center of the map to each point can be calculated using the haversine formula: http://rbrundritt.spaces.live.com/default.aspx?_c01_BlogPart=blogentry&_c=BlogPart&handle=cns!E7DBA9A4BFD458C5!317

The following code is an example of how to go about doing this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
    <title>Find Nearest to Me</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
    <script>
        var map = null;
        var earthRadius = 6367; //radius in km

        function GetMap()
        {
            map = new VEMap(‘myMap’);
            map.LoadMap();
        }  
        function getLatLong(layer, resultsArray, places, hasMore, veErrorMessage)
        {   
            var origin = map.GetCenter();

            if(places.length>0)
            {
                var location = places[0];
                var distance = haversineDistance(origin,location.LatLong);
                for(var i=1;i<places.length;i++)
                {
                    var d = haversineDistance(origin,places[i].LatLong);
                    if(d < distance)
                    {
                        location = places[i];
                        distance = d;
                    }
                }
                map.SetMapView([location.LatLong]);
            }   
        }           

        function FindLoc()
          {
            map.Find(null, document.getElementById(‘txtWhere’).value, null,
                null, 0, 20, false, false, false, false, getLatLong);
          }
        function haversineDistance(latlong1,latlong2)
        {
            var lat1 = DegtoRad(latlong1.Latitude);
            var lon1 = DegtoRad(latlong1.Longitude);
            var lat2 = DegtoRad(latlong2.Latitude);
            var lon2 = DegtoRad(latlong2.Longitude);

            var dLat = lat2-lat1;
            var dLon = lon2-lon1;
            var cordLength = Math.pow(Math.sin(dLat/2),2)+Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dLon/2),2);
            var centralAngle = 2 * Math.atan2(Math.sqrt(cordLength), Math.sqrt(1- cordLength));
            return earthRadius * centralAngle;
        }
        function DegtoRad(x)
        {
            return x*Math.PI/180;
        }

    </script>
</head>

<body onload="GetMap();">
    <CENTER>
        <INPUT id="txtWhere" type="text" name="txtWhere" value=>
        <INPUT id="find" type="button" value="Find"  onclick="FindLoc();"><br/>
        <div id=’myMap’ style="position:relative; width:600px; height:400px;"></div>
    </CENTER>
</body>
</html>

Advertisements

One thought on “Find Location Nearest to You

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s