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));
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With