How to get the expected results in bingmaps SpatialDataService.GeoDataAPIManager.getBoundary()

Surya 6 Reputation points

I have used getBoundary() to get the polygons for my country datasource. For 'BE' I expect the Belgium coordinates, instead am getting the coordinates for Togo. Similarly am getting different results for 'ES', 'SK' and 'IN' as well. Here I have hardcoded the countries datasource. But in real time, the datasource will subject to change dynamically based on user inputs. it can be either a city, state/county or country. so, we set the entity type dynamically using first data. Is there any way to the get the expected results say Belgium for BE, India for IN and Spain for ES.?

function GetMap() {
        var map = new Microsoft.Maps.Map('#myMap', {

        //Create an array of locations to get the boundaries of.
        var Countries = ['AU', 'BE', 'ES', 'SK', 'IN'];
        var type = '';
        var locs = [];
        Microsoft.Maps.loadModule(['Microsoft.Maps.SpatialDataService', 'Microsoft.Maps.Search'], () => {
            var search = new Microsoft.Maps.Search.SearchManager(map);
                where: Countries[0], count: 10, callback: (geocodeResult) => {
                    type = geocodeResult.results[0].entityType; //tslint:disable-line:max-line-length

                () => {
                    var geoDataRequestOptions = {
                        entityType: type,
                        getAllPolygons: true,
                        getEntityMetadata: true
                    Microsoft.Maps.SpatialDataService.GeoDataAPIManager.getBoundary(Countries, geoDataRequestOptions, map,
                            (data) => {
                                var value = data.location;
                                if (data.results.length > 0 && data.results[0].Polygons !== null) {
                                    var dataBounds = Microsoft.Maps.LocationRect.fromShapes(data.results[0].Polygons);
                                    var loc = new Microsoft.Maps.Location(,;
                                    for (var i = 0; i < data.results[0].Polygons.length; i++) {
                                            fillColor: new Microsoft.Maps.Color(0.5,255,192,203),
                                            strokeColor: '#FFB6C1',
                                        data.results[0].Polygons[i].metadata = {
                                            shapeName: value,
                                    map.setView({ bounds: Microsoft.Maps.LocationRect.fromLocations(locs), padding: 80 });

                }, 100);


Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
382 questions
{count} votes

1 answer

Sort by: Most helpful
  1. rbrundritt 8,156 Reputation points Microsoft Employee

    A few things:

    • For your issue, BE is being geocoded as "Bé, Togo" which is a neighborhood in "Lome, Togo". But since you set the entity type to country, its returnign the boundary for Togo. Bing Maps geocoder often sets the most precise entity type item as the first result. For example, if it has a city and country result with equal accuracy score, the city will almost always be ordered first since it is more precise. This is done as Bing Maps is designed with the needs of a search engine in mind. Using Bing Maps there really isn't a good way to better handle this than to have the user select the result they want. Perhaps having an autosuggest experience would be good.
    • A separate issue in your code that will occur for many users, currently you are a 100ms timeout to wait for the geocode request to complete. The geocoding service will not return a response that fast 100% of the time for all users. Put the code for calling the GeoData API into the callback of the geocode search to ensure the GeoData API is called after the geocode call.

    Another option if you are open to using other platforms is to try Azure Maps. Boundary data is retrieved in a bit of a different way. With Azure Maps you have to geocode your query first and if the result has a boundary, an unique boundary ID will be provided which can be used to retrieve that boundary using the boundary service. This means that the boundary is always of the same entity type as the query. Additionally, the geocoder in Azure Maps works a bit different from Bing Maps and orders results differently and works a bit better with well structured queries. Here is a sample app that has a search bar you can use to try this out and see if it better meets your needs:

    2 people found this answer helpful.
    0 comments No comments