问题描述
<maps:MapControl x:Name="Map"
<maps:MapItemsControl x:Name="mapItems">
<maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImageSourceUri}"
Loaded="Image_Loaded"
maps:MapControl.Location="{Binding Location }">
<Image.RenderTransform>
<TransformGroup>
<RotateTransform Angle="{Binding Rotate.Angle}"
CenterX="{Binding Rotate.CenterX}"
CenterY="{Binding Rotate.CenterY}"/>
<TranslateTransform X="{Binding Translate.X}"
Y="{Binding Translate.Y}"/>
<ScaleTransform ScaleX="{Binding Scale.ScaleX}"
ScaleY="{Binding Scale.ScaleY}" />
</TransformGroup>
</Image.RenderTransform>
</Image>
</StackPanel>
</DataTemplate>
</maps:MapItemsControl.ItemTemplate>
</maps:MapItemsControl>
</maps:MapControl>
这是用于设置ItemSource的相关C#代码:
public class InterestPoint
{
public Uri ImageSourceUri { get; set; }
public Geopoint Location { get; set; }
public RotateTransform Rotate { get; set; }
public TranslateTransform Translate { get; set; }
public Point CenterPoint { get; set; }
public ScaleTransform Scale { get; set; }
}
public sealed partial class MainPage : Page
{
private List<InterestPoint> points = new List<InterestPoint>();
private void Map_Loaded(object sender,RoutedEventArgs e)
{
points.Add(new InterestPoint
{
ImageSourceUri = new Uri("ms-appx:///Assets/my_position_red.png"),Location = new Geopoint(new BasicGeoposition
{
Latitude = 0,Longitude = 0,Altitude = 0
},AltitudeReferenceSystem.Terrain),Rotate = new RotateTransform
{
Angle = 00,CenterX = 187 / 2,CenterY = 0
},Translate = new TranslateTransform
{
X = 0,Y = 0
},Scale = new ScaleTransform
{
ScaleX = 0.3,ScaleY = 0.3
}
});
mapItems.ItemsSource = points;
}
}
此代码中有两个问题:
- 位置绑定无效。我可以设置图像URI,旋转度等,但是更改位置不会在地图中移动图像。我已经解决了获取图像对象并直接设置位置的问题,但这不能解决第二个问题
- AltitudeReferenceSystem.Terrain似乎没有应用于Geopoint位置,因此图像似乎不在地形级别,因此放大和缩小似乎像在3D视图中一样围绕该点移动。
预先感谢您的任何建议。 斯特凡诺
编辑:我尝试了一些在网上找到的示例,但遇到了同样的问题。
这是一张缩放比例较低的图片,您可以在主要道路下方看到红点:
放大这是真实位置,比主要道路高出很多
解决方法
位置绑定无效。我可以设置图像URI,旋转度等,但是更改位置不会在地图中移动图像。我已经解决了获取图像对象并直接设置位置的问题,但这不能解决第二个问题
问题是您尚未为Geopoint
属性实现INotifyPropertyChanged
接口,当值更改时它不会更新ui。
AltitudeReferenceSystem.Terrain似乎没有应用到Geopoint位置,因此图像似乎不在地形级别,因此放大和缩小似乎像在3D视图中一样围绕该点移动。
源自官方文档remark部分。
从地理位置API返回的用于定位的海拔参考系统可能取决于GPS / GNSS无线电硬件。大多数现代硬件将使用大地水准面参考系统提供值,但Map Control API将在Elipsoid系统中返回值。要找出Geopoint对象正在使用哪个对象,请参阅AltitudeReferenceSystem属性。您不应在不复制关联的AltitudeReferenceSystem的情况下复制BasicGeoposition,否则Altitude值将无效并可能产生意外结果。
Location = new Geopoint(new BasicGeoposition
{
Latitude = 0,Longitude = 0,Altitude = 0
},AltitudeReferenceSystem.Terrain),
请注意,您需要使旋转中心与地图中心匹配。 源自Stefano Della Valle解决方案。
在我的情况下,我在图像创建中使用了X和Y来设置翻译:
Translate = new TranslateTransform { X = -55,Y = -45 },