我的Windows Phone中的地图上显示了一条路线。现在我想根据地图上绘制的路线来适应缩放。
我做了以下事情:
this.map.SetView(LocationRectangle.CreateBoundingRectangle(locations));
locations
是一个GeoCoordinate
的数组。在横向方向上它可以很好地工作,但是当我切换到纵向方向时,它不会。
--- --- EDIT
以下是相关代码:
在我的OnLoad-Method中调用的draw方法中,我的位置数组在此填充:
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);
}
}
我在OrientationChanged事件中,在Loaded事件中和单击按钮后调用setView()方法,因此您可以轻松地将缩放恢复为默认值。 代码:
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));
}
所有这些在横向模式下都能正常工作,它们都不能在纵向模式下工作,甚至按钮也不能。
答案 0 :(得分:1)
我在调用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);
});
});