使用Bing Maps REST控件查找附近的实体

Jor*_*dan 4 asp.net ajax jquery geolocation bing-maps

我正在尝试使用Bing Maps REST API来查询给定搜索半径内给定名称的所有实体的地址.最终目标是做类似星巴克在这里做的事情:商店定位器,但我使用了Fiddler,看起来他们正在使用6.3 API:/

如果有办法,这似乎很难记录.如果您上传自己的数据,有一些如何执行此操作的示例,但如果您正在搜索应该已经在地图上的本地商家,则不会这样做:示例.这是我到目前为止所尝试的......它正在返回俄勒冈州的星巴克:

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. :( ");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

这是位置查询之前的代码,以防您想知道我在位置查询中使用的位置数据是什么 - 它主要来自用户通过地理定位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. :( ");
            }
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!!

Ala*_*son 6

Bing Maps Locations API用于地理编码 - 即在地图上查找地址或地点.你想要做的是找到东西,然后把它们放在地图上.为此,您需要使用Bing API(而不是Bing Maps API).

Bing电话簿搜索REST服务应该可以满足您的需求 - 这里有一个示例:http://msdn.microsoft.com/en-us/library/dd251030.aspx 电话簿搜索的每个结果都有一个纬度和经度属性您可以使用在地图上创建图钉.