none
SetView(LocationRect) problem RRS feed

  • Question

  • Hi,

    I think I've found a problem with the SetView method when it is provided with a LocationRect.  As I understand it, this method should set both the position and the zoom of the map to show the area specified by the LocationRect.  It seems however that the method makes an incorrect assumption about the size of the map’s viewport.  For a specific LocationRect, the zoom level is always the same regardless of the size of the map.

    The following code demonstrates the issue:

    var smallMap = new Map();
    smallMap.Width = 200;
    smallMap.Height = 200;
    PanelMain.Children.Add(smallMap);
                
    var bigMap = new Map();
    bigMap.Width = 500;
    bigMap.Height = 500;
    PanelMain.Children.Add(bigMap);
    
    var points = new LocationCollection { 
        new Location(51.514216, -0.086144),
        new Location(51.509567, -0.132453)
    };
                
    var rect = GetLocationsRect(points);
    
    smallMap.SetView(rect);
    bigMap.SetView(rect);

    Is this a bug or am I misunderstanding what SetView should do?  If it is working correctly, how should I determine the zoom level to set so that both points are shown within the viewport on various size maps?

    Thanks,

    Jon


    Monday, July 2, 2012 8:33 AM

Answers

  • SetView uses the current width and height when determining the view. The problem is that we do not get notified of a size changed until the next layout pass which happens asynchronously after this code has already been run so it uses the previous size until then. Both newly created maps have this default value and so both end up with that view.

    If you need to create the map on the fly you can work around the problem by attaching to Map.SizeChanged and performing the SetView call there and then detaching from the event.

    Monday, July 2, 2012 5:25 PM

All replies

  • Your understanding of SetView is correct; you should get different zoom levels for the two maps. I will investigate and get back to you.

    Monday, July 2, 2012 5:09 PM
  • SetView uses the current width and height when determining the view. The problem is that we do not get notified of a size changed until the next layout pass which happens asynchronously after this code has already been run so it uses the previous size until then. Both newly created maps have this default value and so both end up with that view.

    If you need to create the map on the fly you can work around the problem by attaching to Map.SizeChanged and performing the SetView call there and then detaching from the event.

    Monday, July 2, 2012 5:25 PM
  • I've used Map.SizeChanged and it work great.  Thanks for your help.  :)

    Tuesday, July 3, 2012 7:34 AM