GMap.NET C#WFP:我可以将自定义UserControl添加到GMapControl叠加层中吗?
项目:c#,wpf,.net4.0,Gmap.Net.Presentation 1.7.1。GMap.NET C#WFP:我可以将自定义UserControl添加到GMapControl叠加层中吗?
我拥有的一切:从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接口,允许我的小部件添加到地图。
任何建议,提示,帮助?
P.S.如果我解决了这个问题,我会在这里发布。我想这对其他人很有帮助。
此外,我发现了很多关于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 UserControl上面绘制。但我希望在GMapControl中有一些内部机制。
我认为解决的办法是比你尝试过什么容易,你可以从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;
}
}
此标记实例给你(在项目内的UserControl
)访问UI性能的自己CustomerMarkerElement
内:
<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>
缺点是,据我所知是没有办法中有这样的符合MVVM的方式(例如,在项目模板中定义自定义标记)。
非常感谢。这是正确的答案。我只是创建我自己的CustomMarker,并在其中更改Shape。 – Binakot
我不知道GMap,但我的感觉是,应该很容易将任何UIElement派生的控件添加到地图并通过某种附加属性指定其位置。如果这是不可能的,你应该尝试另一种控制,例如Bing地图或XAML地图控件。他们有一个带有ItemTemplate(或ItemContainerStyle)的MapItemsControl,你可以在其中放置任何UIElement。这将是MVVM。 – Clemens
感谢您的咨询。现在我试图在GMap中找到MapItemsControl的模拟。如果不存在,我不会找到其他解决方案 - 要更改我的地图控件。 – Binakot