Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom to fit in WP7 Bing Maps control using LocationCollection?

How can I zoom the Microsoft.Phone.Controls.Maps.Map control to the correct zoom level on Windows Phone 7?

I have a LocationCollection of GeoCoordinates and I calculated the Center myself, but now how do I calculate the correct zoom level to fit the LocationCollection?

P.S. Is there an out of the box method to calculate the center of GeoCoordinates so I don't have to calculate it myself?

EDIT: I've found another fine solution: http://4mkmobile.com/2010/09/quick-tip-position-a-map-based-on-a-collection-of-pushpins/

map.SetView(LocationRect.CreateLocationRect(points));

like image 712
Buju Avatar asked Jan 14 '11 17:01

Buju


People also ask

How do I edit Bing Maps?

Update your Bing Maps Dev Center informationSign in to the Bing Maps Dev Center. Click Update or view account details to view your account details, and then click Edit. Make your changes to the account information, and then click Save.


1 Answers

You can use the following code to calculate the LocationRect that bounds a set of points, and then pass the LocationRect to the SetView() method on the map control:

var bounds = new LocationRect(
    points.Max((p) => p.Latitude),
    points.Min((p) => p.Longitude),
    points.Min((p) => p.Latitude),
    points.Max((p) => p.Longitude));
map.SetView(bounds);

The map control handles animating from the current position to the new location.

NOTE: You'll need a using statement for System.Linq to get the Min and Max methods.

like image 116
Derek Lakin Avatar answered Nov 23 '22 19:11

Derek Lakin