Distance from target postcode
-
Tuesday, June 19, 2012 9:48 AM
Hi
I am currently updating an existing Bingmap application for a Retailer.
The client who has a large presence in the UK and a number of stores, would like to include the distance of the store from the target postcode.
So for example, if a client were to search for stores in the postcode "W45DL", then the list of stores available should include a distance(in miles) from the postcode "W45DL".
The client is using the Bingmap REST service for queries and the format of the query string is
https://spatial.virtualearth.net/REST/v1/data/<Key>/<DataStoreName>/<DataStore>?$callback=QueryCallBack&$filter=availableForIsc%20Eq%20true&jsonp=QueryCallBack" + "&key=" + key + "&spatialFilter=nearby(" + cpLat + "," + cpLon + "," + nearByKilometers + ")&$select=store_id,Latitude,Longitude,name,AddressLine,Locality,CountryRegion,PostalCodephone&$skip=0&$format=json&callback=QueryCallBack" + "&$top=" + maxNumerOfStoresToDisplay
Is there a way to modify the above Query String to return the distance from the target postcode or do i need to calculate the distance using the Long/Lat?
- Edited by hindustani_india Tuesday, June 19, 2012 9:49 AM
All Replies
-
Tuesday, June 19, 2012 11:00 AMOwner
Because a $filter is being used in the query you will not be able to get a distance returned. If the $filter was not used you could just add __Distance to the $select paramater. What you can do is easily calculate the distance to each location as you loop through the results to create the pushpins using the Haversine forumla:
function Haversine(lat1, lon1, lat2, lon2){ var R = 6371; // km var dLat = (lat2-lat1)*Math.PI/180; var dLon = (lon2-lon1)*Math.PI/180; var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLon/2) * Math.sin(dLon/2); var c = 2 * Math.asin(Math.sqrt(a)); return R * c; }http://rbrundritt.wordpress.com
- Marked As Answer by Richard_BrundrittMicrosoft Employee, Owner Monday, June 25, 2012 12:36 PM
-
Tuesday, June 26, 2012 12:51 PMThanks. That helped

