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:!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" "">
    <title>Find Nearest to Me</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src=""></script>
        var map = null;
        var earthRadius = 6367; //radius in km

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

                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;

        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;


<body onload="GetMap();">
        <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>


One thought on “Find Location Nearest to You

Leave a Reply

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

You are commenting using your 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