我正在使用面向MVVM的GreatMaps.NET库(https://greatmaps.codeplex.com/)分叉,但我的问题归结为使用自定义画布作为ItemsControl的ItemsPanel的一个基本问题。从本质上说,该映射是一个ItemsControl,MapCanvas将其子图放置在以下位置:
protected override Size ArrangeOverride(Size arrangeSize)
{
foreach (UIElement child in Children)
{
PointLatLng position = new PointLatLng(GetTop(child), GetLeft(child));
GMapControl map = Owner as GMapControl;
if (map != null)
{
GPoint p = map.FromLatLngToLocal(position);
p.Offset(-(long)(map.MapTranslateTransform.X + child.DesiredSize.Width * 0.5), -(long)(map.MapTranslateTransform.Y + child.DesiredSize.Height * 0.5));
Rect rect = new Rect(p.X, p.Y, child.DesiredSize.Width, child.DesiredSize.Height);
child.Arrange(rect);
}
}
return arrangeSize;
}这是因为我的ItemContainerStyle将每个地图项绑定到ViewModel中的纬度和经度,如下所示(以及作为占位符设置为99的ZIndex ):
<Setter Property="Canvas.Left" Value="{Binding Longitude}" />
<Setter Property="Canvas.Top" Value="{Binding Latitude}" />
<Setter Property="Canvas.ZIndex" Value="{Binding ZIndex}" />我使用DataTemplates和DataType字段来调整每个元素的显示方式,该部分正在正常工作。
有一些元素(例如地图上路线的线条),我需要的不是点元素,而是子元素序列。为此,我遵循了该模式,并创建了一个DataTemplate,其中我使用另一个MapCanvas绑定到与其所有者相同的MapControl (在这里,MapOverlay只是MapCanvas的一个副本,它覆盖OnRender以在其子级之间画线):
<DataTemplate DataType="{x:Type viewModels:RouteViewModel}">
<ItemsControl ItemsSource="{Binding Locations}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<wpf:MapOverlay Name="MapOverlay" Owner="{Binding Path=., RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type wpf:GMapControl}}}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Longitude}" />
<Setter Property="Canvas.Top" Value="{Binding Latitude}" />
<Setter Property="Canvas.ZIndex" Value="{Binding ZIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Fill="Blue" Stroke="Black" Width="10" Height="10"></Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>为了使绑定工作正常,RouteViewModel仍然有自己的Lat/Lng,似乎只有当我将它们放置在沿着路线的点列表的初始点时才能工作:
public double Latitude => Locations.Select(loc => loc.Latitude).FirstOrDefault();
public double Longitude => Locations.Select(loc => loc.Longitude).FirstOrDefault();摘要:
问题:当我的元素最初正确定位时,缩放映射(通过映射控件触发InvalidateVisual和UpdateLayout )不会导致在嵌套MapOverlay上调用ArrangeOverride。这导致路线不再正确地定位--它不缩放。
对于为什么安排无效没有滴入嵌套的MapOverlay和/或关于如何修复它的提示,有什么建议吗?
增编:只有当地图最初定位在其第一个元素时,路径才被正确地放置--整个路线的纬度/经度是一个独立的问题,我欢迎大家的想法。
发布于 2016-06-30 16:26:43
其实不是你的问题的答案,但它可能会给你一个大致的想法,如何显示一条路线,即一组由线连接的圆圈。
下面的示例使用XAML MapControl库,但很可能是基于具有某种MapItemsControl (例如MS地图)的任何其他映射库来实现的。
其想法是拥有一个组成您的路由的Location实例的集合。您将拥有一个MapPolyline并将其Locations属性绑定到路由点集合。然后将一个MapItemsControl放在上面,并将其ItemsSource绑定到同一个集合。注意,ItemTemplate使用带有EllipseGeometries的路径控件,而不是椭圆,因为它们集中在点位置,而椭圆是顶/左对齐的。
<map:MapPanel>
<map:MapPolyline Locations="{Binding RouteLocations}"
Stroke="DarkBlue" StrokeThickness="3"/>
<map:MapItemsControl ItemsSource="{Binding RouteLocations}">
<map:MapItemsControl.ItemContainerStyle>
<Style TargetType="map:MapItem">
<Setter Property="map:MapPanel.Location" Value="{Binding}"/>
</Style>
</map:MapItemsControl.ItemContainerStyle>
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<Path Fill="Red">
<Path.Data>
<EllipseGeometry RadiusX="5" RadiusY="5"/>
</Path.Data>
</Path>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
</map:MapPanel>如果路由点不是Location的,则可以在两个绑定中使用绑定转换器将路由点类型转换为Location。
https://stackoverflow.com/questions/38126113
复制相似问题