Reputation: 595
I have a route displayed on a map in my Windows Phone. Now I want to fit the zoom based on the route which is drawn on the map.
I did the following:
this.map.SetView(LocationRectangle.CreateBoundingRectangle(locations));
locations
is an array of GeoCoordinate
s. In landscape orientation it works perfectly, but when I switch to portrait orientation it doesn`t.
---EDIT---
Here is the relevant code:
In my draw method, which is called in my OnLoad-Method, my locations array gets filled here:
double[,] givenCoordinates;
private MapOverlay overlay;
private Microsoft.Phone.Maps.Controls.MapLayer layer = new Microsoft.Phone.Maps.Controls.MapLayer();
private List<GeoCoordinate> locations = new List<GeoCoordinate>();
private void drawMap()
{
try
{
bool centerPosition = false;
for (int i = 0; i < givenCoordinates.GetLength(0); i++)
{
GeoCoordinate tmpCoord;
GeoCoordinate centerPoint = new GeoCoordinate(0, 0);
Image image = new Image();
BitmapImage myImage = new BitmapImage(new Uri(@"Images\pushpin.png", UriKind.Relative));
image.Width = 40;
image.Height = 40;
image.Opacity = 10;
image.Source = myImage;
if (givenCoordinates[i, 0] != 0 && givenCoordinates[i, 1] != 0)
{
tmpCoord = new GeoCoordinate(givenCoordinates[i, 0], givenCoordinates[i, 1]);
if (!centerPosition)
{
centerPoint = new GeoCoordinate(givenCoordinates[i, 0], givenCoordinates[i, 1]);
centerPosition = true;
}
overlay = new MapOverlay
{
GeoCoordinate = tmpCoord,
Content = image,
PositionOrigin = new System.Windows.Point(0.5, 1)
};
layer.Add(overlay);
locations.Add(tmpCoord);
}
}
delMap.Layers.Add(layer);
Microsoft.Phone.Maps.Controls.MapPolyline line = new Microsoft.Phone.Maps.Controls.MapPolyline();
}
catch (Exception ex)
{
MessageBox.Show("Error loading the map!");
MessageBox.Show(ex.Message);
}
}
I call the setView() Method in my OrientationChanged event, in the Loaded event and after a button click, so you can easily restore zoom to default. Code:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.delMap.SetView(LocationRectangle.CreateBoundingRectangle(locations));
}
private void btn_default_zoom_Click(object sender, RoutedEventArgs e)
{
this.delMap.SetView(LocationRectangle.CreateBoundingRectangle(locations));
}
private void PhoneApplicationPage_OrientationChanged_1(object sender, OrientationChangedEventArgs e)
{
delMap.SetView(LocationRectangle.CreateBoundingRectangle(locations));
}
All of this works fine in landscape mode, none of them work in portrait mode, not even the button click.
Upvotes: 2
Views: 253
Reputation: 3683
I tried your code with this little change on when calling the drawMap
//just for testing a list of locations
givenCoordinates = new double[3, 2];
givenCoordinates[0, 0] = 23.4896;
givenCoordinates[0, 1] = 12.1392;
givenCoordinates[1, 0] = 23.4835;
givenCoordinates[1, 1] = 12.1794;
givenCoordinates[2, 0] = 23.5153;
givenCoordinates[2, 1] = 12.1516;
drawMap();
await Task.Run(async () =>
{
Thread.Sleep(1);
Dispatcher.BeginInvoke(() =>
{
delMap.SetView(LocationRectangle.CreateBoundingRectangle(locations), MapAnimationKind.Linear);
});
});
Upvotes: 1