<!-- HERE ARE THE SEARCH BOX WHERE WE ENTER A NEW LOCATION FOR SEARCH -->
<div id='printoutPanel'></div>
<div id='searchBoxContainer'><input type= 'text' id= 'searchBox' style="width:500px"/></div>
<div id='myMap' style='width: 600px; height: 600px;'></div>
<script type='text/javascript'>
function loadMapScenario() {
var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
//credentials: 'HERE YOU CAN ENTER YOUR BING MAP KEY ',
credentials: '<<CREDENTIALS HERE>>',
center: new Microsoft.Maps.Location(47.606209, -122.332071),
zoom: 12
});
Microsoft.Maps.loadModule('Microsoft.Maps.AutoSuggest', function () {
var options = {
maxResults: 4,
map: map
};
// HERE WE ARE SETTING A NEW LOCATION WHEN WE SEARCH FOR ANY C OR R
var manager = new Microsoft.Maps.AutosuggestManager(options);
manager.attachAutosuggest('#searchBox', '#searchBoxContainer', selectedSuggestion);
});
function selectedSuggestion(suggestionResult) {
map.entities.clear();
map.setView({ bounds: suggestionResult.bestView });
var pushpin = new Microsoft.Maps.Pushpin(suggestionResult.location);
map.entities.push(pushpin);
// IF YOU WANT TO SHOW THE LONGITUDE AND LATITUDE ON THE MAP WHICH YOU WANT TO SEARCH THEN JUST UNCOMMENT THE BELOW 4 LINES
// document.getElementById('printoutPanel').innerHTML =
// 'Suggestion: ' + suggestionResult.formattedSuggestion +
// '<br> Lat: ' + suggestionResult.location.latitude +
// '<br> Lon: ' + suggestionResult.location.longitude;
}
}
</script>
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?branch=release&callback=loadMapScenario' async defer></script>
最佳答案
自动建议响应在 suggestResult.address 属性中包含此信息。以下是自动建议响应的文档:https://msdn.microsoft.com/en-US/library/mt712672.aspx
https://msdn.microsoft.com/en-us/library/mt750287.aspx
或者,如果您不想使用自动建议,您也可以使用搜索模块,该模块将对单个查询进行地理编码,而不是在您键入时进行地理编码。以下是一些文档/示例:
http://www.bing.com/api/maps/sdkrelease/mapcontrol/isdk#searchByAddress+JS
https://msdn.microsoft.com/en-us/library/mt750534.aspx
https://msdn.microsoft.com/en-us/library/mt712846.aspx
最后一个选项是直接访问 Bing map REST 服务。有关如何使用各种 JavaScript 框架(例如 jQuery)执行此操作的示例,请参见:https://msdn.microsoft.com/en-US/library/mt793281.aspx
关于javascript - 如何使用 JavaScript 从 BING map API 获取地址(城市、州、国家、邮政编码)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41764354/