简体   繁体   中英

How to get address(city,state, country, zip code) from BING map API using JavaScript?

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

The autosuggest response contains this information in suggestionResult.address property. Here is documentation for the autosuggest response: https://msdn.microsoft.com/en-US/library/mt712672.aspx

https://msdn.microsoft.com/en-us/library/mt750287.aspx

Alternatively, if you don't want to use autosuggest you can also use the search module which will geocode individual queries, not as you type. Here are some docs/examples:

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

One final option is to access the Bing Maps REST services directly. Examples of how to do this with various JavaScript frameworks such as jQuery can be found here: https://msdn.microsoft.com/en-US/library/mt793281.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM