none
How to notify a user if they pans/zoom out of a certain area? RRS feed

  • Question

  • How can we get an Event if user leaves a certain area (defined by polygon coordinates) on map? For example: if a user pans/zoom-out outside of a certain state, he/she gets an alert telling that they are outside coverage area.

    Thanks in advance.

    Wednesday, October 3, 2012 4:17 AM

Answers

  • Ah, right. Thought my code sample seemed a bit too simple. Here you go:

    // Microsoft.Maps.LocationRect.fromEdges(north:number, west:number, south:number, east:number)
    var coverageArea = Microsoft.Maps.LocationRect.fromEdges(65, -10, 30, 10);
    
    Microsoft.Maps.Events.addHandler(map, 'viewchangeend', notifyUser);
    
    function notifyUser(e){
    	if(!coverageArea.contains(map.getCenter())){
    		alert("You have left the coverage area");
    	}
    }

    http://rbrundritt.wordpress.com

    • Marked as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Unmarked as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Proposed as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Marked as answer by SSR1656 Wednesday, October 3, 2012 10:56 PM
    Wednesday, October 3, 2012 8:16 PM

All replies

  • You can monitor the center point of the map by attaching to the view change end event and checking to see if the user has left the area in question. If they have you can notify the user using an alert or some other notification. Here is a simple code sample:

    Microsoft.Maps.Events.addHandler(map, 'viewchangeend', notifyUser);
    
    function notifyUser(e){
    	alert("You have left the coverage area");
    }


    http://rbrundritt.wordpress.com

    Wednesday, October 3, 2012 4:37 PM
  • Thanks for the reply. Actually I am very new to Maps, so excuse me if I am asking something very obvious.

    How can we check if the center point of map is outside of certain set of cordinates? In my case this set of cordinates creates a polygen.

    Wednesday, October 3, 2012 5:35 PM
  • Ah, right. Thought my code sample seemed a bit too simple. Here you go:

    // Microsoft.Maps.LocationRect.fromEdges(north:number, west:number, south:number, east:number)
    var coverageArea = Microsoft.Maps.LocationRect.fromEdges(65, -10, 30, 10);
    
    Microsoft.Maps.Events.addHandler(map, 'viewchangeend', notifyUser);
    
    function notifyUser(e){
    	if(!coverageArea.contains(map.getCenter())){
    		alert("You have left the coverage area");
    	}
    }

    http://rbrundritt.wordpress.com

    • Marked as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Unmarked as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Proposed as answer by Ricky_Brundritt Wednesday, October 3, 2012 8:17 PM
    • Marked as answer by SSR1656 Wednesday, October 3, 2012 10:56 PM
    Wednesday, October 3, 2012 8:16 PM
  • Thanks a lot. This is what I was looking for, though I finally used Microsoft.Maps.LocationRect.fromLocations(list of locations/array)

    You saved me a lot of time. :-) Thanks again.

    Wednesday, October 3, 2012 10:56 PM