I’m attempting to use the Bing Maps REST API to query the addresses of all entities, of a given name, within a given search radius. The end goal is to do something like what Starbucks does here: Store Locator, but I used Fiddler and it looks like they are using the 6.3 API :/
This seems very poorly documented if there is a way to do this. There are some examples of how to do this if you upload your own data, but not if you’re searching for local businesses that should already be on the map: Examples. Here’s what I have tried so far…it is returning Starbucks, Oregon:
var query = 'starbucks';
_map.getCredentials(function (credentials) {
$.getJSON('http://dev.virtualearth.net/REST/V1/Locations/' + query + '?key=' + credentials + '&lat=' + position.coords.latitude + '&long=' + position.coords.longitude + '&limit=25&jsonp=?&s=1',
function (result) {
if (result.resourceSets[0].address != 'undefined') {
var address = result.resourceSets[0].address;
alert(address);
}
else {
$("#results").html("Oops! It appears one or more of the addresses you entered are incorrect. :( ");
}
});
});
This is the code that precedes the location query in case you’re wondering what the location data is that I’m using in the location query – it’s essentially from the user’s location via the geolocation API:
var _map;
$(document).ready(function () {
if (Modernizr.geolocation) {
$(".geofallback").hide();
}
else {
$(".geofallback").show();
}
$.post("Home/Key", { "func": "Key" }, function (data) {
// Create a Bing map
_map = new Microsoft.Maps.Map(document.getElementById("map"),
{ credentials: data, mapTypeId: Microsoft.Maps.MapTypeId.ordnanceSurvey });
});
// Get the current position from the browser
if (!navigator.geolocation) {
$("#results").html("This browser doesn't support geolocation, please enter an address");
}
else {
navigator.geolocation.getCurrentPosition(onPositionReady, onError);
}
});
function onPositionReady(position) {
// Apply the position to the map
var location = new Microsoft.Maps.Location(position.coords.latitude,
position.coords.longitude);
_map.setView({ zoom: 18, center: location });
// Add a pushpin to the map representing the current location
var pin = new Microsoft.Maps.Pushpin(location);
_map.entities.push(pin);
var query = 'starbucks';
_map.getCredentials(function (credentials) {
$.getJSON('http://dev.virtualearth.net/REST/V1/Locations/' + query + '?key=' + credentials + '&lat=' + position.coords.latitude + '&long=' + position.coords.longitude + '&limit=25&jsonp=?&s=1',
function (result) {
if (result.resourceSets[0].address != 'undefined') {
var address = result.resourceSets[0].address;
alert(address);
}
else {
$("#results").html("Oops! It appears one or more of the addresses you entered are incorrect. :( ");
}
});
});
}
Any help would be greatly appreciated!!
The Bing Maps Locations API is for geocoding – i.e. finding addresses or places on the map. What you want to do is find things and then place them on the map. For this, you need to use the Bing API (not the Bing Maps API).
The Bing Phonebook search REST service should give you what you need – there’s an example here: http://msdn.microsoft.com/en-us/library/dd251030.aspx
Each result from the phonebook search has a Latitude and Longitude property which you can use to create a pushpin on the map.