我正在向我的 MapControl 添加一个 MapElement,但是当我旋转 MapControl 时,我的 MapElement 会随之旋转。是否可以在元素上设置固定标题?
MapElement不继承自UIElement,这是RenderTransform
. 所以不可能在 MapElement 上使用 RenderTransform。
但作为一种解决方法,您可以考虑使用MapItemsControl根据需要在地图上放置任何控件或图像,并且可以在旋转 MapControl 时旋转或平移图像。
MainPage.Xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center">
<Maps:MapControl
Width="500"
Height="500"
x:Name="myMap"
ZoomInteractionMode="GestureAndControl"
TiltInteractionMode="GestureAndControl"
MapServiceToken="MapServiceToken">
<Maps:MapItemsControl x:Name="mapItems">
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImageSourceUri}"
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}"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</StackPanel>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
</Maps:MapControl>
<Button Name="myAdd" Click="myAdd_Click">Click Me to Add Element</Button>
<Button Name="btnRotate" Click="btnRotate_Click">Click Me to Rotate Element</Button>
</StackPanel>
</Grid>
MainPage.xaml.cs:
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 sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private List<InterestPoint> InitInterestPoints(BasicGeoposition location)
{
List<InterestPoint> points = new List<InterestPoint>();
points.Add(new InterestPoint {
ImageSourceUri = new Uri("ms-appx:///Assets/mappin.png"),
Location = new Geopoint(location),
Rotate=new RotateTransform
{
Angle=15,
CenterX=28.5,
CenterY=88
},
Translate=new TranslateTransform
{
X=-28.5,
Y=-90
}
});
return points;
}
private void myAdd_Click(object sender, RoutedEventArgs e)
{
mapItems.ItemsSource = InitInterestPoints(myMap.Center.Position);
}
private void btnRotate_Click(object sender, RoutedEventArgs e)
{
var points = mapItems.ItemsSource as List<InterestPoint>;
points[0].Rotate.Angle += 10;
}
}
这是效果:
这是整个演示的链接:MapElementRotationSample。
注意:添加图像时以左上点为锚点。所以需要根据图片大小对图片进行平移。(我的图片是57*90,所以需要平移(-28.5,-88)让针点为中心点)。