嵌入GIS地图服务
Posted doom555
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入GIS地图服务相关的知识,希望对你有一定的参考价值。
通过开源GMap.NET.Core实现地图服务的应用
1. 引用GMap.NET.Core.dll和GMap.NET.WindowsPresentation.dll(开源的,可以网上下载源代码自己编辑、编译,下载地址:https://files.cnblogs.com/files/Doom555/DLL.zip)
2. 创建GMapProvider
1 GMapProvider m_CurrentProvider; 2 public GMapProvider CurrentProvider 3 { 4 set 5 { 6 if (m_CurrentProvider != value) 7 { 8 m_CurrentProvider = value; 9 m_Gmap.MapProvider = m_CurrentProvider; 10 } 11 } 12 get { return m_CurrentProvider; } 13 }
CurrentProvider = GMapProviders.GoogleChinaHybridMap;
3. 在窗体中添加GMap控件
xmlns:gmap="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation"
<gmap:GMapControl Grid.Row="1" Name="m_Gmap" Grid.Column="1"></gmap:GMapControl>
4. 在窗体初始化完之后设置GMap的基本参数
1 m_Gmap.MapProvider = CurrentProvider; 2 3 m_Gmap.MinZoom = 1; //最小缩放 4 m_Gmap.MaxZoom = 19; //最大缩放 5 m_Gmap.ShowCenter = false; //不显示中心十字点 6 m_Gmap.MouseWheelZoomType = MouseWheelZoomType.MousePositionWithoutCenter;//缩放模式以鼠标为中心 7 m_Gmap.DragButton = MouseButton.Left; //左键拖拽地图 8 //m_Gmap.MouseLeftButtonUp += M_Gmap_MouseLeftButtonUp; 9 //m_Gmap.MouseLeftButtonDown += M_Gmap_MouseLeftButtonDown; 10 //m_Gmap.MouseMove += M_Gmap_MouseMove; 11 //m_Gmap.MouseRightButtonUp += M_Gmap_MouseRightButtonUp; 12 //m_Gmap.OnMapZoomChanged += M_GmapZoomChanged; 13 m_Gmap.Loaded += M_Gmap_Loaded; 14 //// m_Gmap.MouseWheel += M_Gmap_MouseWheel; 15 //m_Gmap.MouseDoubleClick += M_Gmap_MouseDoubleClick; 16 m_Gmap.IgnoreMarkerOnMouseWheel = true; 17 //m_Gmap.CacheLocation = System.Windows.Forms.Application.StartupPath;//指定地图缓存存放路径 18 19 //initAllUElement();//初始化地图元素 20 21 m_Gmap.Manager.Mode = AccessMode.ServerAndCache;//地图加载模式
5. 实现基本的操作
a. 获取当前窗体的地图边界
RectLatLng selectedArea;
PointLatLng leftTop = new PointLatLng(), rightBottom = new PointLatLng();
1 void M_Gmap_Loaded(object sender, RoutedEventArgs e) 2 { 3 selectedArea = m_Gmap.MapViewBound; 4 { 5 leftTop.Lat = selectedArea.LocationTopLeft.Lat; 6 leftTop.Lng = selectedArea.LocationTopLeft.Lng; 7 8 rightBottom.Lat = selectedArea.LocationRightBottom.Lat; 9 rightBottom.Lng = selectedArea.LocationRightBottom.Lng; 10 } 11 double zoomLevel = m_Gmap.Zoom; 12 double[] LT = WGS2CJ.Gaode_GCJ2WGS(leftTop.Lat, leftTop.Lng); 13 double[] RB = WGS2CJ.Gaode_GCJ2WGS(rightBottom.Lat, rightBottom.Lng); 14 15 //FreshNoFlyZone(LT[0], LT[1], RB[0], RB[1], (byte)zoomLevel); 16 }
b. 地图拖拽
1 void M_Gmap_OnMapDrag() 2 { 3 selectedArea = m_Gmap.MapViewBound; 4 { 5 leftTop.Lat = selectedArea.LocationTopLeft.Lat; 6 leftTop.Lng = selectedArea.LocationTopLeft.Lng; 7 8 rightBottom.Lat = selectedArea.LocationRightBottom.Lat; 9 rightBottom.Lng = selectedArea.LocationRightBottom.Lng; 10 } 11 double zoomLevel = m_Gmap.Zoom; 12 double[] LT = WGS2CJ.Gaode_GCJ2WGS(leftTop.Lat, leftTop.Lng); 13 double[] RB = WGS2CJ.Gaode_GCJ2WGS(rightBottom.Lat, rightBottom.Lng); 14 15 ZoomLevel = (byte)zoomLevel;//用于更新右下角显示 16 17 MapCenterChanged(m_Gmap.Position.Lat, m_Gmap.Position.Lng, 0); 18 19 //if (ZoomLevel >= 13 && m_HeightCheck.IsChecked == true) //还有一个条件 按下了开关 20 //{ 21 // if (thresholdHeight == 0) 22 // { 23 // MessageBox.Show("请输入正确的检测高度!"); 24 // return; 25 // } 26 // ThreadPool.QueueUserWorkItem(SetDemThread); 27 28 //} 29 30 //FreshNoFlyZone(LT[0], LT[1], RB[0], RB[1], ZoomLevel); 31 }
c.创建自己的标记控件
1 <UserControl x:Class="GoogleMap.CustomMarker" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:GoogleMap" 7 mc:Ignorable="d" 8 Name="m_CustomMarker" 9 d:DesignHeight="300" d:DesignWidth="300"> 10 <UserControl.Resources> 11 <ResourceDictionary> 12 <local:MarkMarginConverter x:Key="MarkMarginConverter"></local:MarkMarginConverter> 13 </ResourceDictionary> 14 </UserControl.Resources> 15 <Grid > 16 <Image Name="icon" Height="{Binding HW ,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Width="{Binding HW ,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Source="{Binding ImgSrc,ElementName=m_CustomMarker,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsHitTestVisible="True" HorizontalAlignment="Left" VerticalAlignment="Top"> 17 <!--<Image.CacheMode> 18 <BitmapCache ></BitmapCache> 19 </Image.CacheMode>--> 20 <Image.RenderTransform> 21 <RotateTransform Angle="{Binding Path=Heading,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/> 22 </Image.RenderTransform> 23 </Image> 24 <TextBlock Name="lable" Visibility="{Binding Path=bLable,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Text="{Binding MarkLable,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Foreground="Red" FontFamily="Arial" Background="WhiteSmoke" Opacity="1" FontSize="15" FontWeight="Bold" Padding="1" Margin="{Binding HW,ElementName=m_CustomMarker,Mode=OneWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MarkMarginConverter}}" HorizontalAlignment="Left" VerticalAlignment="Top"></TextBlock> 25 </Grid> 26 </UserControl>
1 using GMap.NET.WindowsPresentation; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 6 using System.Text; 7 using System.Threading.Tasks; 8 using System.Windows; 9 using System.Windows.Controls; 10 using System.Windows.Controls.Primitives; 11 using System.Windows.Data; 12 using System.Windows.Documents; 13 using System.Windows.Input; 14 using System.Windows.Media; 15 using System.Windows.Media.Imaging; 16 using System.Windows.Navigation; 17 using System.Windows.Shapes; 18 19 namespace GoogleMap 20 { 21 22 public delegate void UElementIsCatchedCallBackHandle(bool b); 23 public delegate void UElementIsDragedCallBackHandle(CustomMarker b); 24 /// <summary> 25 /// CustomMarker.xaml 的交互逻辑 26 /// </summary> 27 public partial class CustomMarker : UserControl, INotifyPropertyChanged 28 { 29 public UElementIsCatchedCallBackHandle UElementIsCatchedCallBack { set; get; } 30 public UElementIsDragedCallBackHandle UElementIsDragedCallBack { set; get; } 31 public UElementIsDragedCallBackHandle UElementIsRightClickedCallBack { set; get; } 32 public UElementIsDragedCallBackHandle UElementIsEndDragCallBack { set; get; } 33 Popup Popup; 34 Label Label; 35 Border border; 36 public GMapMarker Marker { set; get; } 37 GoogleMapPage MainWindow; 38 int m_nID = -1; 39 public int nID 40 { 41 set 42 { 43 m_nID = value; 44 if (m_Dsr.Contains("#")) 45 { 46 //如果是航点传入的 47 MarkLable = "#" + (m_nID+1).ToString(); 48 Label.Content = "航点#" + (m_nID + 1).ToString(); 49 } 50 if (m_Dsr.Contains("降") || m_Dsr.Contains("减")) 51 { 52 //如果是降落航点传入的 53 MarkLable = "#" + (m_nID + 1).ToString(); 54 Label.Content = m_Dsr; 55 } 56 } 57 get { return m_nID; } 58 } 59 public CustomMarker(GoogleMapPage window, GMapMarker marker, string title, string src, int ID, int fontSize = 12) 60 { 61 InitializeComponent(); 62 this.InitializeComponent(); 63 ImgSrc = src; 64 this.MainWindow = window; 65 this.Marker = marker; 66 67 Popup = new Popup(); 68 Label = new Label(); 69 border = new Border(); 70 Dsr = title; 71 72 // this.SizeChanged += new SizeChangedEventHandler(AirPointMarkerDemo_SizeChanged); 73 this.MouseEnter += new MouseEventHandler(MarkerControl_MouseEnter); 74 this.MouseLeave += new MouseEventHandler(MarkerControl_MouseLeave); 75 this.MouseMove += new MouseEventHandler(CustomMarkerDemo_MouseMove); 76 this.MouseLeftButtonUp += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonUp); 77 this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonDown); 78 this.MouseRightButtonUp += CustomMarker_MouseRightButtonUp; 79 80 Popup.AllowsTransparency = true; 81 Popup.Placement = PlacementMode.Mouse; 82 { 83 Label.Foreground = Brushes.White; 84 Label.Padding = new Thickness(10, 5, 10, 5); 85 Label.FontSize = fontSize; 86 Label.Content = title; 87 border.Child = Label; 88 } 89 { 90 border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#99000000")); 91 border.BorderThickness = new Thickness(2); 92 border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#cc000000")); 93 border.CornerRadius = new CornerRadius(5); 94 } 95 border.Child = Label; 96 Popup.Child = border; 97 98 nID = ID; //最后再更新,用于更新文字 99 } 100 101 private void CustomMarker_MouseRightButtonUp(object sender, MouseButtonEventArgs e) 102 { 103 UElementIsRightClickedCallBack?.Invoke(this); 104 } 105 106 int m_HW; 107 public int HW 108 { 109 get { return m_HW; } 110 set 111 { 112 m_HW = value; 113 OnPropertyChanged(new PropertyChangedEventArgs("HW")); 114 } 115 } 116 string m_ImgSrc; 117 public string ImgSrc 118 { 119 get { return m_ImgSrc; } 120 set 121 { 122 m_ImgSrc = value; 123 OnPropertyChanged(new PropertyChangedEventArgs("ImgSrc")); 124 } 125 } 126 float m_Heading = 0; 127 public float Heading 128 { 129 get { return m_Heading; } 130 set 131 { 132 if (m_Heading != value) 133 { 134 m_Heading = value; 135 //更新offset值,根据heading计算 136 Marker.Offset = new Point( -HW/2*Math.Sqrt(2) * Math.Sin((135 + m_Heading) / 180 * Math.PI), HW/2 * Math.Sqrt(2) * Math.Cos((135 + m_Heading) / 180 * Math.PI)); 137 // = RotateImage(UavImg, value); //如果角度改变了,更新图片角度 138 OnPropertyChanged(new PropertyChangedEventArgs("Heading")); 139 } 140 } 141 } 142 string m_Dsr = string.Empty; //飞机描述 143 public string Dsr 144 { 145 get { return m_Dsr; } 146 set 147 { 148 m_Dsr = value; 149 Label.Content = m_Dsr; 150 OnPropertyChanged(new PropertyChangedEventArgs("Dsr")); 151 } 152 } 153 string m_MarkLable = string.Empty; //地图常显 154 public string MarkLable 155 { 156 get { return m_MarkLable; } 157 set 158 { 159 m_MarkLable = value; 160 OnPropertyChanged(new PropertyChangedEventArgs("MarkLable")); 161 } 162 } 163 164 Visibility m_bLable = Visibility.Collapsed; //文本可见性 默认不可见 165 public Visibility bLable 166 { 167 get { return m_bLable; } 168 set 169 { 170 m_bLable = value; 171 OnPropertyChanged(new PropertyChangedEventArgs("bLable")); 172 } 173 } 174 public event PropertyChangedEventHandler PropertyChanged; 175 public void OnPropertyChanged(PropertyChangedEventArgs e) 176 { 177 if (PropertyChanged != null) 178 PropertyChanged(this, e); 179 } 180 181 //int[] beforePosition = new int[2]; 182 //private DateTime timemousedown; 183 //private int timemillSecond = 200; 184 //bool isMouseDrag = false; 185 186 void CustomMarkerDemo_SizeChanged(object sender, SizeChangedEventArgs e) 187 { 188 Marker.Offset = new System.Windows.Point(e.NewSize.Width / 2, e.NewSize.Height); 189 } 190 191 void CustomMarkerDemo_MouseMove(object sender, MouseEventArgs e) 192 { 193 if (e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured && canMove) 194 { 195 //MessageBox.Show(DateTime.Now.ToShortTimeString()); 196 System.Windows.Point p = e.GetPosition(MainWindow.m_Gmap); 197 Marker.Position = MainWindow.m_Gmap.FromLocalToLatLng((int)(p.X), (int)(p.Y)); 198 199 UElementIsCatchedCallBack?.Invoke(true); 200 UElementIsDragedCallBack?.Invoke(this); 201 } 202 canMove = true; 203 } 204 bool canMove = true; 205 void CustomMarkerDemo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 206 { 207 if (!IsMouseCaptured ) 208 { 209 //timemousedown = DateTime.Now; 210 canMove = false; 211 Mouse.Capture(this); 212 UElementIsCatchedCallBack?.Invoke(true); 213 214 //beforePosition[0] = (int)MainWindow.m_Gmap.FromLatLngToLocal(Marker.Position).X; 215 //beforePosition[1] = (int)MainWindow.m_Gmap.FromLatLngToLocal(Marker.Position).Y; 216 } 217 else 218 { 219 UElementIsCatchedCallBack?.Invoke(false); 220 } 221 } 222 223 void CustomMarkerDemo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 224 { 225 if (IsMouseCaptured) 226 { 227 //if (timemousedown.AddMilliseconds(timemillSecond) > DateTime.Now) 228 //{ 229 // Marker.Position = MainWindow.m_Gmap.FromLocalToLatLng(beforePosition[0], beforePosition[1]); 230 //} 231 232 Mouse.Capture(null); 233 UElementIsEndDragCallBack?.Invoke(this); 234 } 235 } 236 237 void MarkerControl_MouseLeave(object sender, MouseEventArgs e) 238 { 239 Marker.ZIndex -= 10000; 240 Popup.IsOpen = false; 241 } 242 243 void MarkerControl_MouseEnter(object sender, MouseEventArgs e) 244 { 245 Marker.ZIndex += 10000; 246 Popup.IsOpen = true; 247 } 248 249 } 250 251 public class MarkMarginConverter : FrameworkElement, IValueConverter 252 { 253 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 254 { 255 int? hw = value as int?; 256 double HW = 0; 257 if (hw != null) 258 { 259 HW = (double)hw; 260 } 261 return new Thickness(HW, HW / 2, 0, 0); 262 } 263 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 264 { 265 throw new NotSupportedException(); 266 } 267 } 268 }
d. 初始化地图上的标记样式
1 GMapPolygon extendGMapPolygonRec = new GMapPolygon(new List<PointLatLng>()); 2 3 GMapPolygon downloadRec = new GMapPolygon(new List<PointLatLng>()); 4 5 CustomMarker uavCus = null; 6 GMapMarker UAVPoint = new GMapMarker(new PointLatLng()); 7 GMapRoute UavRoute = new GMapRoute(new List<PointLatLng>()); 8 GMapRoute NewUavRoute = new GMapRoute(new List<PointLatLng>()); 9 GMapRoute UavTargetPath = new GMapRoute(new PointLatLng[2]); 10 GMapRoute UavTargetPath = new GMapRoute(new PointLatLng[2]); 11 GMapRoute AirLine = new GMapRoute(new List<PointLatLng>()); 12 GMapRoute LandAirLine = new GMapRoute(new List<PointLatLng>()); 13 GMapRoute LandConnetToAirLine = new GMapRoute(new PointLatLng[2]);//连接线只有两个点 14 15 GMapRoute PreviewAirLine = new GMapRoute(new List<PointLatLng>()); 16 CustomMarker enterMark; 17 CustomMarker exitMark; 18 GMapMarker EnterPoint = new GMapMarker(new PointLatLng()); 19 GMapPolygon surveyPolygon = new GMapPolygon(new List<PointLatLng>()); 20 GMapRoute surveyPolyline = new GMapRoute(new List<PointLatLng>()); //边界线 21 GMapRoute surveyDaiLine = new GMapRoute(new List<PointLatLng>()); //带线 22 GMapRoute surveyPolygonGuidLine = new GMapRoute(new List<PointLatLng>()); //测区引导线 23 GMapPolygon EllipseFenceMarker = new GMapPolygon(new List<PointLatLng>()); 24 GMapPolygon ElectricFencePolygon = new GMapPolygon(new List<PointLatLng>()); 25 GMapPolygon PolygonPermissionZone = new GMapPolygon(new List<PointLatLng>()); 26 GMapPolygon CirclePermissionMarker = new GMapPolygon(new List<PointLatLng>()); 27 GMapRoute LineDis = new GMapRoute(new List<PointLatLng>()); 28 29 30 31 /// <summary> 32 /// 初始化Gmap地图显示的元素 33 /// </summary> 34 void initAllUElement() 35 { 36 //外扩多边形 37 extendGMapPolygonRec.M_Opacity = 0.4; 38 extendGMapPolygonRec.M_Fill = Brushes.DarkGreen; 39 extendGMapPolygonRec.M_Stroke = Brushes.Black; 40 extendGMapPolygonRec.M_SThickness = 2; 41 extendGMapPolygonRec.ZIndex = 80; 42 extendGMapPolygonRec.M_IsHitTest = false; 43 m_Gmap.Markers.Add(extendGMapPolygonRec); 44 45 //下载矩形 46 downloadRec.M_Opacity = 0.5; 47 downloadRec.M_Fill = Brushes.Blue; 48 downloadRec.M_Stroke = Brushes.Blue; 49 downloadRec.M_SThickness = 3; 50 downloadRec.ZIndex = 80; 51 downloadRec.M_IsHitTest = false; 52 m_Gmap.Markers.Add(downloadRec); 53 54 //无人机和轨迹 55 uavCus = new CustomMarker(this, UAVPoint, "无人机", "img/uav.png", 0) { Heading = 0, HW = 32 }; 56 UAVPoint.Shape = uavCus; 57 UAVPoint.ZIndex = 100; 58 // UAVPoint.Offset = new Point(16, 16); //会转航向的Mark会自动获取Offset 59 UAVPoint.M_IsHitTest = false; 60 UavRoute.M_Stroke = Brushes.Blue; 61 UavRoute.M_SThickness = 2; 62 UavRoute.M_Opacity = 1; 63 UavRoute.ZIndex = 95; 64 UavRoute.M_IsHitTest = false; 65 m_Gmap.Markers.Add(UAVPoint); 66 m_Gmap.Markers.Add(UavRoute); 67 68 NewUavRoute.M_Stroke = Brushes.Blue; 69 NewUavRoute.M_SThickness = 2; 70 NewUavRoute.M_Opacity = 1; 71 NewUavRoute.ZIndex = 95; 72 NewUavRoute.M_IsHitTest = false; 73 m_Gmap.Markers.Add(NewUavRoute); 74 75 UavTargetPath.M_Stroke = (SolidColorBrush)brConv.ConvertFromString("#00ff00"); ; 76 UavTargetPath.M_SThickness = 1; 77 UavTargetPath.ZIndex = 96; 78 UavTargetPath.M_Opacity = 1; 79 m_Gmap.Markers.Add(UavTargetPath); 80 81 //饭盒位置 82 CustomMarker polygonMarker = new CustomMarker(this, RtkBasePoint, "基准站", "img/ditu_fanhe.png", 0) { HW = 32 }; 83 RtkBasePoint.Offset = new Point(-16, -16); 84 RtkBasePoint.Shape = polygonMarker; 85 m_Gmap.Markers.Add(RtkBasePoint); 86 87 //航线 88 AirLine.M_Stroke = Brushes.Red; 89 AirLine.M_SThickness = 2; 90 AirLine.M_Opacity = 1; 91 AirLine.ZIndex = 90; 92 AirLine.M_IsHitTest = true; 93 m_Gmap.Markers.Add(AirLine); 94 95 //降落航线 96 LandAirLine.M_Stroke = Brushes.Violet;//(SolidColorBrush)brConv.ConvertFromString("#003366"); //24位颜色 , 97 LandAirLine.M_SThickness = 2; 98 LandAirLine.M_Opacity = 1; 99 LandAirLine.ZIndex = 10; 100 LandAirLine.M_IsHitTest = true; 101 m_Gmap.Markers.Add(LandAirLine); 102 103 //任务 降落 连接虚线 104 LandConnetToAirLine.M_Stroke = Brushes.Red; 105 LandConnetToAirLine.M_SThickness = 2; 106 LandConnetToAirLine.M_Opacity = 1; 107 LandConnetToAirLine.ZIndex = 90; 108 LandConnetToAirLine.M_Dash = new DoubleCollection() { 10, 10 }; 109 LandConnetToAirLine.M_IsHitTest = true; 110 m_Gmap.Markers.Add(LandConnetToAirLine); 111 112 //预览航线 113 PreviewAirLine.M_Stroke = Brushes.Yellow; 114 PreviewAirLine.M_SThickness = 1; 115 PreviewAirLine.M_Opacity = 1; 116 PreviewAirLine.ZIndex = 90; 117 PreviewAirLine.M_IsHitTest = true; 118 m_Gmap.Markers.Add(PreviewAirLine); 119 120 enterMark = new CustomMarker(this, EnterPoint, "进入点", "img/EnterAirline.png", 0) { Heading = 0, HW = 32 }; 121 EnterPoint.Shape = enterMark; 122 EnterPoint.Offset = new Point(-16, -16); 123 EnterPoint.M_IsHitTest = false; 124 m_Gmap.Markers.Add(EnterPoint); 125 126 exitMark = new CustomMarker(this, ExitPoint, "退出点", "img/ExitAirline.png", 0) { Heading = 0, HW = 32 }; 127 ExitPoint.Shape = exitMark; 128 ExitPoint.Offset = new Point(-16, -16); 129 ExitPoint.M_IsHitTest = false; 130 m_Gmap.Markers.Add(ExitPoint); 131 132 //多边形测区 133 surveyPolygon.M_Opacity = 0.2; 134 surveyPolygon.M_Fill = Brushes.Red; 135 surveyPolygon.ZIndex = 80; 136 surveyPolygon.M_IsHitTest = false; 137 m_Gmap.Markers.Add(surveyPolygon); 138 139 //多边形测区边界线 140 surveyPolyline.M_Stroke = Brushes.Red; 141 surveyPolyline.M_SThickness = 1; 142 surveyPolyline.M_Opacity = 1; 143 surveyPolyline.ZIndex = 80; 144 m_Gmap.Markers.Add(surveyPolyline); 145 146 surveyDaiLine.M_Stroke = Brushes.Red; 147 surveyDaiLine.M_SThickness = 5; 148 surveyDaiLine.M_Opacity = 0.3; 149 surveyDaiLine.ZIndex = 80; 150 surveyDaiLine.M_IsHitTest = false; 151 m_Gmap.Markers.Add(surveyDaiLine); 152 153 //引导线 154 surveyPolygonGuidLine.M_Stroke = Brushes.Red; 155 surveyPolygonGuidLine.M_Fill = Brushes.Red; 156 surveyPolygonGuidLine.M_SThickness = 1; 157 surveyPolygonGuidLine.M_Opacity = 1; 158 surveyPolygonGuidLine.M_Dash = new DoubleCollection() { 10, 6.18 }; 159 surveyPolygonGuidLine.M_IsHitTest = false;//不拦截鼠标事件 160 surveyPolygonGuidLine.ZIndex = 85; 161 m_Gmap.Markers.Add(surveyPolygonGuidLine); 162 163 //圆形电子围栏 164 EllipseFenceMarker.M_Stroke = Brushes.Yellow; 165 EllipseFenceMarker.M_Fill = Brushes.Orange; 166 EllipseFenceMarker.M_Opacity = 0.4; 167 EllipseFenceMarker.M_SThickness = 3; 168 EllipseFenceMarker.M_IsHitTest = false; 169 EllipseFenceMarker.ZIndex = 70; 170 m_Gmap.Markers.Add(EllipseFenceMarker); 171 172 //多边形电子围栏 173 ElectricFencePolygon.M_Stroke = Brushes.Yellow; 174 ElectricFencePolygon.M_Fill = Brushes.Orange; 175 ElectricFencePolygon.M_Opacity = 0.4; 176 ElectricFencePolygon.M_SThickness = 3; 177 ElectricFencePolygon.M_IsHitTest = false; 178 ElectricFencePolygon.ZIndex = 70; 179 m_Gmap.Markers.Add(ElectricFencePolygon); 180 181 PolygonPermissionZone.M_Stroke = Brushes.Blue; 182 PolygonPermissionZone.M_Fill = Brushes.DeepSkyBlue; 183 PolygonPermissionZone.M_Opacity = 0.5; 184 PolygonPermissionZone.M_SThickness = 3; 185 PolygonPermissionZone.M_IsHitTest = false; 186 PolygonPermissionZone.ZIndex = 82; 187 m_Gmap.Markers.Add(PolygonPermissionZone); 188 189 CirclePermissionMarker.M_Stroke = Brushes.Blue; 190 CirclePermissionMarker.M_Fill = Brushes.DeepSkyBlue; 191 CirclePermissionMarker.M_Opacity = 0.5; 192 CirclePermissionMarker.M_SThickness = 3; 193 CirclePermissionMarker.M_IsHitTest = false; 194 CirclePermissionMarker.ZIndex = 82; 195 m_Gmap.Markers.Add(CirclePermissionMarker); 196 197 //测距 198 LineDis.M_Stroke = Brushes.Orange; 199 LineDis.M_SThickness = 2; 200 LineDis.M_Opacity = 1; 201 m_Gmap.Markers.Add(LineDis); 202 }
6. 实现地图搜索
1 Dictionary<string, string> dicLstSpace = new Dictionary<string, string>(); 2 public void SearchKeyValue(string keyValue) 3 { 4 //新逻辑 5 #region 6 try 7 { 8 #region 9 //http://api.tianditu.gov.cn/search?postStr={"keyWord":"超市","level":"11","mapBound":"116.04577,39.70307,116.77361,40.09583","queryType":"1","count":"20","start":"0"}&type=query&tk=您的密钥 10 11 string preUrl = "http://api.tianditu.gov.cn/search?postStr="; 12 string postStr = "{ "keyWord":"" + keyValue + "","mapBound":"-180,-90,180,90","level":"11","queryType":"7","count":"10","start":"0" }&type=query&tk=46686f9ceb244fed0b535a996ce2d5f8"; 13 string Url = preUrl + postStr; 14 15 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 16 // request.Timeout = 20000; 17 request.Method = "GET"; 18 request.ContentType = "application/json"; 19 // request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)"; 20 request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (Khtml, like Gecko) Chrome/0.2.149.27 Safari/525.13"; 21 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 22 string resultstring = ""; 23 if (response != null) 24 { 25 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 26 { 27 resultstring = reader.ReadToEnd(); 28 } 29 } 30 if (!string.IsNullOrEmpty(resultstring)) 31 { 32 dicLstSpace.Clear(); 33 JObject strObj = (JObject)JsonConvert.DeserializeObject(resultstring); 34 string resType = strObj["resultType"].ToString(); 35 List<string> lst = new List<string>(); 36 if (resType == "1") 37 { 38 string address, 39 names, 40 lonlat, 41 poisCount = strObj["pois"].ToString(); 42 if (!string.IsNullOrEmpty(poisCount)) 43 { 44 JArray jArray = JArray.Parse(poisCount); 45 for (int i = 0; i < 3; i++) 46 { 47 JObject job = (JObject)jArray[i]; 48 address = job["address"].ToString(); 49 names = job["name"].ToString(); 50 lonlat = job["lonlat"].ToString(); 51 this.Dispatcher.Invoke(new Action(delegate () 52 { 53 lst.Add(names + "(" + address + ")"); 54 55 dicLstSpace.Add(names + "(" + address + ")", lonlat); 56 })); 57 } 58 this.Dispatcher.Invoke(new Action(delegate () 59 { 60 lstSpace.Visibility = Visibility.Visible; 61 lstSpace.ItemsSource = lst; 62 })); 63 } 64 } 65 } 66 67 #endregion 68 } 69 catch (Exception ex) 70 { 71 MessageBox.Show(ex.ToString()); 72 // LogHelper.ErrorLog(LogStr, ex); 73 } 74 75 #endregion 76 }
以上是关于嵌入GIS地图服务的主要内容,如果未能解决你的问题,请参考以下文章