using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms.Markers;
namespace GMapWinFormDemo
{
public partial class MainForm : Form
{
private GMapOverlay objects = new GMapOverlay("objects"); //放置marker的图层
private GMapMarkerImage currentMarker;
private bool isLeftButtonDown = false;
private Timer blinkTimer = new Timer();
public MainForm()
{
InitializeComponent();
try
{
System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com.hk");
}
catch
{
mapControl.Manager.Mode = AccessMode.CacheOnly;
MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
mapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存位置
mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图
mapControl.MinZoom = 2; //最小比例
mapControl.MaxZoom = 17; //最大比例
mapControl.Zoom = 5; //当前比例
mapControl.ShowCenter = false; //不显示中心十字点
mapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图
mapControl.Position = new PointLatLng(32.064,118.704); //地图中心位置:南京
mapControl.OnMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);
mapControl.MouseClick += new MouseEventHandler(mapControl_MouseClick);
mapControl.MouseDown += new MouseEventHandler(mapControl_MouseDown);
mapControl.MouseUp += new MouseEventHandler(mapControl_MouseUp);
mapControl.MouseMove += new MouseEventHandler(mapControl_MouseMove);
mapControl.OnMarkerClick += new MarkerClick(mapControl_OnMarkerClick);
mapControl.OnMarkerEnter += new MarkerEnter(mapControl_OnMarkerEnter);
mapControl.OnMarkerLeave += new MarkerLeave(mapControl_OnMarkerLeave);
mapControl.Overlays.Add(objects);
}
void mapControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left && isLeftButtonDown)
{
if (currentMarker != null)
{
PointLatLng point = mapControl.FromLocalToLatLng(e.X, e.Y);
currentMarker.Position = point;
currentMarker.ToolTipText = string.Format("{0},{1}", point.Lat, point.Lng);
}
}
}