【问题标题】:GMap.NET C# WFP: Can i add custom UserControl into GMapControl overlays?GMap.NET C# WFP:我可以将自定义 UserControl 添加到 GMapControl 覆盖中吗?
【发布时间】:2016-03-01 06:41:18
【问题描述】:

项目:c#、wpf、.net4.0、Gmap.Net.Presentation 1.7.1。

我有什么:

我的自定义 MapControl 类继承自 GMap.NET.WindowsPresentation.GMapControl 类。

public sealed class MapControl : GMapControl
{
    /* Some special data. */

    public MapConrol()
        : base()
    {
        /* Some init actions. */
    }

    /* Overrided and additional methods. */
}

例如,我有一些自定义的 UserControl 类。

代码:

public sealed partial class MapObjectMarkerUiControl : UserControl
{
    /* Some special data. */

    public MapObjectMarkerUiControl()
    {
        /* Some init actions. */
    }

    /* Overrided and additional methods. */
}

Xaml:

<UserControl x:Class="MapCustomControls.MapObjectMarkerUiControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Width="40" Height="40" RenderTransformOrigin="0.5, 0.5">
    <Grid>
        <!-- Some visual controls: text, buttons, etc. -->
    </Grid>
</UserControl>

自定义用户控件示例:

我需要什么:

有什么方法可以参考地理坐标将其添加到地图中?像这样的东西: gmap.AddCustomUserControl(UserControl customMarker, double latitude, double longitude);

也许我应该从其他类继承我的 UserControl 或实现一些允许将我的小部件添加到地图的 Gmap.NET 接口。

有什么建议、提示、帮助吗?

附:如果我解决了这个问题,我会在这里发布。我想这对其他人会很有帮助。

另外,我在 StackOverflow 等上发现了很多关于 GMap 的问题,到处都看到了 Overlay 类。

GMapOverlay markersOverlay = new GMapOverlay("markers");
gmap.Overlays.Add(markersOverlay);

在我的版本中,我没有这个。我已经在 GMap 类中存在内置标记覆盖。

gmap.Markers - ObservableCollection of the GMapMarkers.

而且没有办法创建我自己的叠加层并将其添加到 GMapControl 对象中。

更新 0:

我脑海中的第一个想法。例如,只需通过地图对象的 id 在地图上添加一些特殊标记的 GMapMarkers。 GMapControl 的 OnRender() 找到屏幕上的所有标记,解析它们的 id 并在我的 wpf UserControls 上方绘制。但我希望 GMapControl 中有一些内部机制。

【问题讨论】:

  • 我不知道 GMap,但我的感觉是应该很容易将任何 UIElement 派生控件添加到地图并通过某种附加属性指定其位置。如果这是不可能的,你应该尝试另一个控制,例如必应地图或 XAML 地图控件。他们有一个带有 ItemTemplate(或 ItemContainerStyle)的 MapItemsControl,您可以在其中放置任何 UIElement。它将是 MVVM。
  • 感谢您的建议。现在我试图在 GMap 中找到 MapItemsControl 的模拟。如果不存在并且我不会找到其他解决方案 - 将更改我的地图控件。

标签: c# wpf xaml user-controls gmap.net


【解决方案1】:

我相信解决方案比您尝试的要容易,您可以从GMapMarker 派生,例如:

class CustomMarker: GMapMarker
{
    public string Description { get; set; }

    public CustomMarker(PointLatLng pos, string description)
            : base(pos)
    {
        Description = description;
        Shape = new CustomMarkerElement();

        ((CustomMarkerElement)Shape).lblDesc.Content = description;            
    }
}

此标记实例使您可以访问自己的 CustomerMarkerElement(项目中的 UserControl)内的 UI 属性:

<UserControl x:Class="WpfApplication3.CustomMarkerElement"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse 
            Fill="DarkKhaki"
            Height="40"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Width="40" />

        <Label 
            x:Name="lblDesc"
            Content="TST" 
            Foreground="White" 
            FontWeight="Bold" 
            HorizontalAlignment="Left" 
            HorizontalContentAlignment="Center" 
            Margin="0,6"
            VerticalAlignment="Top"
            Width="40"/>

    </Grid>
</UserControl>

缺点是 afaik 无法以符合 MVVM 的方式来实现这一点(例如,在项目模板中定义自定义标记)。

【讨论】:

  • 非常感谢。这是正确的答案。我只是创建自己的 CustomMarker 并更改其中的形状。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 2014-08-18
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多