我在 Windows Phone 的 map 上显示了一条路线。现在我想根据 map 上绘制的路线进行缩放。
我做了以下事情:
this.map.SetView(LocationRectangle.CreateBoundingRectangle(locations));
locations
是一个 GeoCoordinate
数组。在横向模式下它工作得很好,但当我切换到纵向模式时它就不行了。
---编辑---
相关代码如下:
在我的 OnLoad-Method 中调用的绘制方法中,我的位置数组在此处填充:
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));
}
所有这些在横向模式下都可以正常工作,但在纵向模式下都无法正常工作,甚至连按钮点击都不行。
最佳答案
我在调用 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);
});
});
关于c# - 纵向模式下的 Windows Phone map.setView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223633/