如何使用JavaScript从BING map API获取地址(城市,州,国家,邮政编码)?

时间:2017-01-20 13:01:43

标签: javascript jquery ajax bing-maps

<!-- 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>

1 个答案:

答案 0 :(得分:1)

autosuggest响应在suggestionResult.address属性中包含此信息。以下是autosuggest响应的文档: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 Maps REST服务。可以在此处找到如何使用各种JavaScript框架(如jQuery)执行此操作的示例:https://msdn.microsoft.com/en-US/library/mt793281.aspx