GMap.NET 学习(一)添加marker和polygon
GMap.NET is great and Powerful, Free, cross platform, open source .NET control. Enable use routing, geocoding, directions and maps from Coogle, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac, Yandex, Mapy.cz, Maps.lt, iKarte.lv, NearMap, OviMap, CloudMade, WikiMapia, MapQuest in Windows Forms & Presentation, supports caching and runs on windows mobile!
这样,自己的代码就可以访问GMap.NET里的类了。因为这个GMap.NET是个用户控件,所以可以添加到工具箱,这样便于在属性窗口设置控件的属性。当然也可以不添加到工具箱,直接在代码里实例化这个控件,但是那样就没法使用属性窗口设置属性了。
要使用这个控件,就必须理解以下几点:
1. What is the map control (GMapControl)? This is the control which renders the map.
2. What is an Overlay (GMapOverlay)? This is a layer on top of the map control. You can have several layers on top of a map, each layer representing, say, a route with stops, a list of stores etc.
3. What are Markers (GMapMarker)? These are the points on a layer, each representing a specific geo location (Lat,Lon) e.g. each drop point on a route.
4. What is a route (GMapRoute)? This is the path or direction between two or more points.
5. WTF are tiles? – well here is something to read…Bing Maps Tile System.
1.向Windows Form添加一个GMap控件
直接把控件从工具箱拖到form上,调整大小,然后把默认名字GMapControl1改为gmap
选择控件,选择属性,打开属性面板,除了控件的通用属性外,还有GMap特定的属性,
CanDragMap-----鼠标右键拖动地图
MarkersEnabled---显示markers
PolygonsEnabled---显示polygon
ShowTileGridLines---显示坐标格网
Zoom, MinZoom, MaxZoom---Google地图的缩放水平从0-18,0是全球范围,18是街道级别,全国级别的话,zoom设为5比较合适。
2.初始化地图
给form添加一个onLoad事件,写入如下代码:
private void Form1_Load(object sender, EventArgs e) { // Initialize map: gmap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance; GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly; gmap.SetCurrentPositionByKeywords("Maputo, Mozambique"); } 现在运行就可以看到地图了。如果无法用鼠标右键拖拽地图和滚轮缩放,检查控件属性设置是否正确。
有如下四种方式设置地图的默认位置:
gmap.SetCurrentPositionByKeywords(“country”) - USA
gmap.SetCurrentPositionByKeywords(“state, country”) – Berlin, Germany
gmap.SetCurrentPositionByKeywords(“province, country”) – Alberta, Canada
gmap.Position = new PointLatLng(-25.971684,32.589759);
CloudMadeMapProvider
GoogleMapProvider – map provider for Google Maps;有很多种类型的地图
OpenCycleMapProvider
OpenStreetMapProvider
WikiMapiaMapProvider
YahooMapProvider
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(-25.966688, 32.580528), newBitmap("c:\\images\\mymarker.png");
最后,把marker添加到overlay,再把overlay添加到map。这些marker可以和任何map provider兼容。
5.添加区域 Adding polygons
以下代码创建一个overlay,并在其中添加一个四边形
GMapOverlay polyOverlay = new GMapOverlay("polygons");
List<PointLatLng> points = new List<PointLatLng>();
points.Add(new PointLatLng(-25.969562,32.585789));
points.Add(new PointLatLng(-25.966205,32.588171));
points.Add(new PointLatLng(-25.968134,32.591647));
points.Add(new PointLatLng(-25.971684,32.589759));
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
polygon.Stroke = new Pen(Color.Red, 1);
polyOverlay.Polygons.Add(polygon);
gmap.Overlays.Add(polyOverlay);
首先创建一个新的overlay,然后,创建一个装多边形顶点的list,使用这个list创建一个GMapPolygon实例,然后用SolidBrush告诉这个polygon如何绘制自己,给笔画选定pen的粗细颜色,最后,把polygon添加到overlay,再把overlay添加到map。
结论:
tip:
如果marker不显示,先查看下属性面板里MarkersEnabled是否设置为true
不联网的话,也可以用离线地图,添加如下代码:
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.CacheOnly;
代码里要添加引用的命名空间:
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.WindowsForms.ToolTips;
转自 http://blog.sina.com.cn/s/blog_819100560101dgng.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。