How to avoid the next search result not affected by previous one after call ".setView" to adjust the view?

Drizzle Wen 20 Reputation points
2023-03-07T02:54:05.0033333+00:00

I have a sample based on the link https://learn.microsoft.com/en-us/bingmaps/v8-web-control/map-control-concepts/search-module-examples/basic-geocode-example and set some custom options.

# Address

1.DE Deutschland
2.Siemensstraße DE Deutschland
3.Siemensstraße DE 73479 Deutschland - Ellwangen
4.Siemensstraße DE 73635 Deutschland - Rudersberg
5.Siemensstraße DE 71254 Deutschland - Ditzingen

# Code Snippet

var searchRequest = {
...,
count: 25,
callback: function(r){
map.setView({ bounds: r.results[0].bestView });
},

...
};

# Search Results

  1. Every first search for above address yields the desired results, below are search text & results.

    Siemensstraße DE 73479 Deutschland

      # Siemensstraße, **73479** Ellwangen, Germany  
    

    Siemensstraße DE 73635 Deutschland

      # Siemensstraße, **73635** Rudersberg, Germany  
      # Siemensstraße, **73635** Oberndorf, Germany  
    

    Siemensstraße DE 71254 Deutschland

      # Siemensstraße, **71254** Ditzingen, Germany
    
  2. But if following below search orders without reload the page after per search, the result was not as expected.

    DE Deutschland -> Siemensstraße DE Deutschland -> Siemensstraße DE 73635 Deutschland

     # Siemensstraße, **10553** Berlin, Germany  
     # Siemensstraße, **10551** Berlin, Germany  
    

It seems calls map.setView function will change the viewport and affects next search's parameter "usermapview", for the same query text, different "usermapview" might lead to different results.
For the second case, only 2 results were returned, It doesn't even include the desired results "Siemensstraße, 73635 Rudersberg, Germany" and "Siemensstraße, 73635 Oberndorf, Germany“

# Request URLs ( second case)
# https://dev.ditu.live.com/REST/v1/Locations/?q=DE%20Deutschland&o=json&jsonp=Microsoft.Maps.NetworkCallbacks.f629a2&key=AmbspgUflTI-maTYRpiwUeF5mA6kXUUfmjykps33RhHYZGUSyE6Up_AySUpHT5AE&maxResults=5&userMapView=23.123920800261175,113.25951504707336,23.134280598833783,113.26927828788757&inclnb=0&incl=&ur=cn&c=en-US
# https://dev.ditu.live.com/REST/v1/Locations/?q=Siemensstra%C3%9Fe%20DE%20Deutschland&o=json&jsonp=Microsoft.Maps.NetworkCallbacks.f6a5e3&key=AmbspgUflTI-maTYRpiwUeF5mA6kXUUfmjykps33RhHYZGUSyE6Up_AySUpHT5AE&maxResults=5&userMapView=43.55492527250473,0.45519447326659446,57.98947250054124,20.450311660766594&inclnb=0&incl=&ur=cn&c=en-US
# https://dev.ditu.live.com/REST/v1/Locations/?q=Siemensstra%C3%9Fe%20DE%2073635%20Deutschland&o=json&jsonp=Microsoft.Maps.NetworkCallbacks.f704fc&key=AmbspgUflTI-maTYRpiwUeF5mA6kXUUfmjykps33RhHYZGUSyE6Up_AySUpHT5AE&maxResults=5&userMapView=52.52608813430596,13.31912459872878,52.53979358856163,13.338651080357199&inclnb=0&incl=&ur=cn&c=en-US

Our scenarios is when user enter text in fields like "street", "zipcode" in a form, then make a geocode call to request the matched locations, the request orders may be like below:

  1. enter "Siemensstraße” in street field -> sending a geocode request
  2. enter "73635“ in zipcode filed -> sending a geocode request
  3. every geocode response will be use to fill the other fields in form such as "longitude", "latitude" ...

Is there any way to always get desired locations?

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

Accepted answer
  1. rbrundritt 20,921 Reputation points Microsoft Employee Moderator
    2023-03-07T15:29:27.74+00:00

    It sounds like the main issue is that the search manager uses the map view in the userMapView parameter by default. To remove the impact this has you can set the bounds option in the geocode request to a global bounding box. For example:

    var searchRequest = {
    	where: query,
    	bounds: Microsoft.Maps.LocationRect.fromEdges(85.5, -180, -85.5, 180),
    	callback: function (r) {
    		//Add the first result to the map and zoom into it.
    		if (r && r.results && r.results.length > 0) {
    			var pin = new Microsoft.Maps.Pushpin(r.results[0].location);
    			map.entities.push(pin);
    
    			map.setView({ bounds: r.results[0].bestView });
    		}
    	},
    	errorCallback: function (e) {
    		//If there is an error, alert the user about it.
    		alert("No results found.");
    	}
    };
    
    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.