2011-07-07 28 views
4

我試圖使用Bing Maps REST API查詢給定搜索半徑內給定名稱的所有實體的地址。最終目標是做類似於星巴克在這裏做的事情:Store Locator,但我使用了Fiddler,看起來他們使用的是6.3 API:/使用Bing Maps REST控件查找附近實體

如果有辦法做到這一點,那麼這似乎很差。如果您上傳自己的數據,有一些如何做到這一點的例子,但如果您正在尋找本應該已經在地圖上的本地企業,則不會有這樣的例子:Examples。這是我迄今爲止嘗試過......它返回星巴克,俄勒岡州:

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. :("); 
     } 
    }); 
}); 

這是在前面的情況下的位置查詢你想知道的位置數據是什麼,我使用的代碼位置查詢 - 它本質上來自用戶通過地理定位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. :("); 
      } 
     }); 
    }); 
} 

任何幫助將不勝感激!

回答

6

必應地圖位置API用於地理編碼 - 即查找地圖上的地址或地點。你想要做的是找到東西,然後將它們放置在地圖上。爲此,您需要使用Bing API(而不是Bing Maps API)。

的冰薄搜索REST服務應該給你你需要的東西 - 這裏有一個例子:http://msdn.microsoft.com/en-us/library/dd251030.aspx 從電話簿搜索每個結果都有一個緯度,哪些是你可以用它來創建地圖上的圖釘經度屬性。

+0

謝謝阿拉斯泰爾!我給這個一杆。這應該工作,一如既往,我一定會確認這是被接受的答案,併發佈一個工作代碼示例 – Jordan

+0

正如注:電話簿REST服務不包括開放時間和商業網站Url,但Bing Maps Search SOAP服務確實包含該數據如果您需要它,出於某種原因,Bing地圖搜索API不會提供一個REST接口(奇怪的是,所有其他服務都可以)。 – Junto

+0

現在只需留言給任何人看:這不再存在。 – Seiyria

0

謝謝阿拉斯泰爾 - 完美的工作! 下面是代碼的一個工作示例,其中大部分代碼稍微改編自Alastair與我聯繫的Microsoft代碼:http://msdn.microsoft.com/en-us/library/dd251030.aspx

這是在MVC3的上下文中使用Bing Maps API與Bing地址簿API結合使用,因此其他代碼用於將地圖調整爲用戶的地理位置,然後用於查找最近的x號碼(最大值每次搜索25個結果),無論你在尋找什麼 - 在這種情況下,咖啡! (「Home/GetBingMapsKey」,...和$ .post(「Home/GetBingKey」,...)在ASP.NET MVC3控制器的上下文中,它只是返回鍵...不,它使得密鑰的安全,因爲不幸的是沒有辦法通過在使用REST API的報頭的關鍵...

var _map; 
var _appId; 

$(document).ready(function() { 
    if (Modernizr.geolocation) { 
     $(".geofallback").hide(); 
    } 
    else { 
     $(".geofallback").show(); 
    } 
    $.post("Home/GetBingMapsKey", { "func": "GetBingMapsKey" }, function (data) { 
     // Create a Bing map 
     _map = new Microsoft.Maps.Map(document.getElementById("map"), 
      { credentials: data }); //, mapTypeId: Microsoft.Maps.MapTypeId.ordnanceSurvey 
    }); 
    $.post("Home/GetBingKey", { "func": "GetBingKey" }, function (data) { 
     _appId = data; 
    }); 
    // 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); 
     navigator.geolocation.getCurrentPosition(Search, 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); 
} 

function onError(err) { 
    switch (err.code) { 
     case 0: 
      alert("Unknown error :("); 
      break; 
     case 1: 
      alert("Location services are unavailable per your request."); 
      break; 
     case 2: 
      alert("Location data is unavailable."); 
      break; 
     case 3: 
      alert("The location request has timed out. Please contact support if you continue to experience issues."); 
      break; 
    } 
} 

function Search(position) { 

    var requestStr = "http://api.bing.net/json.aspx?" 

     // Common request fields (required) 
     + "AppId=" + _appId 
     + "&Query=starbucks" 
     + "&Sources=Phonebook" 

     // Common request fields (optional) 
     + "&Version=2.2" 
     + "&Market=en-us" 
     + "&UILanguage=en" 
     + "&Latitude=" + position.coords.latitude 
     + "&Longitude=" + position.coords.longitude 
     + "&Radius=100.0" 
     + "&Options=EnableHighlighting" 

     // Phonebook-specific request fields (optional) 

     // Phonebook.Count max val is 25 
     + "&Phonebook.Count=25" 
     + "&Phonebook.Offset=0" 
     // YP = Commercial Entity, WP = Residential 
     + "&Phonebook.FileType=YP" 
     + "&Phonebook.SortBy=Distance" 

     // JSON-specific request fields (optional) 
     + "&JsonType=callback" 
     + "&JsonCallback=?"; 

    $.getJSON(requestStr, function (data) { 
     SearchCompleted(data); 
    }); 
    //var requestScript = document.getElementById("searchCallback"); 
    //requestScript.src = requestStr; 
} 

function SearchCompleted(response) { 
    var errors = response.SearchResponse.Errors; 
    if (errors != null) { 
     // There are errors in the response. Display error details. 
     DisplayErrors(errors); 
    } 
    else { 
     // There were no errors in the response. Display the 
     // Phonebook results. 
     DisplayResults(response); 
    } 
} 

function DisplayResults(response) { 
    var output = document.getElementById("output"); 
    var resultsHeader = document.createElement("h4"); 
    var resultsList = document.createElement("ul"); 
    output.appendChild(resultsHeader); 
    output.appendChild(resultsList); 

    var results = response.SearchResponse.Phonebook.Results; 

    // Display the results header. 
    resultsHeader.innerHTML = "Bing API Version " 
      + response.SearchResponse.Version 
      + "<br />Phonebook results for " 
      + response.SearchResponse.Query.SearchTerms 
      + "<br />Displaying " 
      + (response.SearchResponse.Phonebook.Offset + 1) 
      + " to " 
      + (response.SearchResponse.Phonebook.Offset + results.length) 
      + " of " 
      + response.SearchResponse.Phonebook.Total 
      + " results<br />"; 

    // Display the Phonebook results. 
    var resultsListItem = null; 
    var resultStr = ""; 
    for (var i = 0; i < results.length; ++i) { 
     resultsListItem = document.createElement("li"); 
     resultsList.appendChild(resultsListItem); 
     resultStr = results[i].Business 
       + "<br />" 
       + results[i].Address 
       + "<br />" 
       + results[i].City 
       + ", " 
       + results[i].StateOrProvince 
       + "<br />" 
       + results[i].PhoneNumber 
       + "<br />Average Rating: " 
       + results[i].UserRating 
       + "<br /><br />"; 

     // Replace highlighting characters with strong tags. 
     resultsListItem.innerHTML = ReplaceHighlightingCharacters(
       resultStr, 
       "<strong>", 
       "</strong>"); 
    } 
} 

function ReplaceHighlightingCharacters(text, beginStr, endStr) { 
    // Replace all occurrences of U+E000 (begin highlighting) with 
    // beginStr. Replace all occurrences of U+E001 (end highlighting) 
    // with endStr. 
    var regexBegin = new RegExp("\uE000", "g"); 
    var regexEnd = new RegExp("\uE001", "g"); 

    return text.replace(regexBegin, beginStr).replace(regexEnd, endStr); 
} 

function DisplayErrors(errors) { 
    var output = document.getElementById("output"); 
    var errorsHeader = document.createElement("h4"); 
    var errorsList = document.createElement("ul"); 
    output.appendChild(errorsHeader); 
    output.appendChild(errorsList); 

    // Iterate over the list of errors and display error details. 
    errorsHeader.innerHTML = "Errors:"; 
    var errorsListItem = null; 
    for (var i = 0; i < errors.length; ++i) { 
     errorsListItem = document.createElement("li"); 
     errorsList.appendChild(errorsListItem); 
     errorsListItem.innerHTML = ""; 
     for (var errorDetail in errors[i]) { 
      errorsListItem.innerHTML += errorDetail 
        + ": " 
        + errors[i][errorDetail] 
        + "<br />"; 
     } 

     errorsListItem.innerHTML += "<br />"; 
    } 
}