Jquery 使用Bing Maps REST控件查找附近的实体
我试图使用Bing Maps REST API查询给定搜索半径内给定名称的所有实体的地址。最终目标是像星巴克在这里做的那样:,但我使用了Fiddler,看起来他们使用的是6.3 API:/ 如果有办法做到这一点的话,这一点似乎记录得非常糟糕。如果您上传自己的数据,但如果您正在搜索应该已经在地图上的本地企业,则有一些示例可以说明如何执行此操作:。以下是我迄今为止尝试过的……它正在返回俄勒冈州的星巴克:Jquery 使用Bing Maps REST控件查找附近的实体,jquery,asp.net,ajax,geolocation,bing-maps,Jquery,Asp.net,Ajax,Geolocation,Bing Maps,我试图使用Bing Maps REST API查询给定搜索半径内给定名称的所有实体的地址。最终目标是像星巴克在这里做的那样:,但我使用了Fiddler,看起来他们使用的是6.3 API:/ 如果有办法做到这一点的话,这一点似乎记录得非常糟糕。如果您上传自己的数据,但如果您正在搜索应该已经在地图上的本地企业,则有一些示例可以说明如何执行此操作:。以下是我迄今为止尝试过的……它正在返回俄勒冈州的星巴克: var query = 'starbucks'; _map.getCredentials(fun
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. :( ");
}
});
});
这是位置查询之前的代码,以防您想知道我在位置查询中使用的位置数据是什么-它基本上是通过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. :( ");
}
});
});
}
任何帮助都将不胜感激 Bing地图位置API用于地理编码,即在地图上查找地址或位置。你要做的是找到东西,然后把它们放在地图上。为此,您需要使用Bing API(而不是Bing地图API) Bing电话簿搜索REST服务应该为您提供所需的服务-这里有一个示例:
电话簿搜索的每个结果都有一个纬度和经度属性,您可以使用该属性在地图上创建图钉。谢谢阿拉斯泰尔-工作得很好! 下面是一个代码的工作示例,其中大多数代码都是从Alastair链接到我的Microsoft代码中稍微修改过来的: 这是在MVC3的上下文中,使用Bing地图API和Bing通讯簿API,因此另一个代码用于根据用户的地理位置调整地图,然后使用它查找您正在查找的任何内容的最近的x数(每次搜索最多25个结果)-在本例中,是咖啡 $.post(“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 />";
}
}
var\u映射;
var_appId;
$(文档).ready(函数(){
if(现代化地理定位){
$(“.geofallback”).hide();
}
否则{
$(“.geofallback”).show();
}
$.post(“Home/GetBingMapsKey”,{“func”:“GetBingMapsKey”},函数(数据){
//创建Bing地图
_map=new Microsoft.Maps.map(document.getElementById(“map”),
{credentials:data});//,mapTypeId:Microsoft.Maps.mapTypeId.ordnanceSurvey
});
$.post(“Home/GetBingKey”,{“func”:“GetBingKey”},函数(数据){
_appId=数据;
});
//从浏览器中获取当前位置
如果(!navigator.geolocation){
$(“#结果”).html(“此浏览器不支持地理位置,请输入地址”);
}
否则{
navigator.geolocation.getCurrentPosition(onPositionReady,onError);
navigator.geolocation.getCurrentPosition(搜索,onError);
}
});
功能位置就绪(位置){
//将位置应用于地图
var location=新的Microsoft.Maps.location(position.coords.latitude,
位置坐标经度);
_setView({zoom:18,center:location});
//向表示当前位置的地图添加图钉
var pin=新的Microsoft.Maps.Pushpin(位置);
_地图。实体。推(销);
}
函数ONERR(错误){
开关(错误代码){
案例0:
警报(“未知错误:(”);
打破
案例1:
警报(“根据您的请求,位置服务不可用。”);
打破
案例2:
警报(“位置数据不可用”);
打破
案例3:
警报(“位置请求已超时。如果您继续遇到问题,请与支持人员联系。”);
打破
}
}
功能搜索(位置){
var requestStr=”http://api.bing.net/json.aspx?"
//通用请求字段(必填)
+“AppId=“+”AppId
+“&Query=starbucks”
+“&Sources=Phonebook”
//公共请求字段(可选)
+“&Version=2.2”
+“&Market=en us”
+“&UILanguage=en”
+“&Latitude=“+position.coords.Latitude”
+“&Longitude=“+position.coords.Longitude”
+“&Radius=100.0”
+“&Options=启用高亮显示”
//电话簿特定请求字段(可选)
//电话簿。计数最大值为25
+“&Phonebook.Count=25”
+“&Phonebook.Offset=0”
//YP=商业实体,WP=住宅
+“&Phonebook.FileType=YP”
+“&Phonebook.SortBy=距离”
//特定于JSON的请求字段(可选)
+“&JsonType=callback”
+“&JsonCallback=?”;
$.getJSON(requestStr,函数(数据){
已完成搜索(数据);
});
//var requestScript=document.getElementById(“searchCallback”);
//requestScript.src=requestStr;
}
功能搜索已完成(响应){
var errors=response.SearchResponse.errors;
如果(错误!=null){
//响应中有错误。显示错误详细信息。
显示错误(错误);
}
否则{
//响应中没有错误。显示
//电话簿结果。
显示结果(响应);
}
}
函数显示结果(响应){
var output=document.getElementById(“输出”);
var resultheader=document.createElement(“h4”);
var resultsList=document.createElement(“ul”);
output.appendChild(resultsHeader);
output.appendChild(resultsList);
var results=response.SearchResponse.Phonebook.results;
//显示结果标题。
resultsHeader.innerHTML=“Bing API版本”
+response.SearchResponse.Version
+“
的电话簿结果”
+response.SearchResponse.Query.SearchTerms
+“
显示”
+(response.SearchResponse.Phonebook.Offset+1)