-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
99 lines (74 loc) · 2.57 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using GMap.NET;
using GMap.NET.MapProviders;
using System;
using System.Device.Location;
using System.Windows.Forms;
namespace Where_Am_I
{
public partial class Form1 : Form
{
private bool markers = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeMap(new Location());
}
private void costumeBtn_Click(object sender, EventArgs e)
{
UpdateMap(new Location(ToLatLng(txtLatitude.Text), ToLatLng(txtLangitude.Text)));
}
private void LocateBtn_Click(object sender, EventArgs e)
{
var location = new Location();
var watcher = new GeoCoordinateWatcher();
//watcher.StatusChanged += (s, ev) =>{};
watcher.PositionChanged += (s, ev) =>
{
if (ev.Position.Location.IsUnknown != true)
{
location.Latitude = ev.Position.Location.Latitude;
location.Langitude = ev.Position.Location.Longitude;
UpdateMap(location);
watcher.Stop();
}
};
watcher.MovementThreshold = 100;
watcher.Start();
}
private void UpdateMap(Location location)
{
InitializeMap(location);
GMap.NET.WindowsForms.GMapOverlay markers = new GMap.NET.WindowsForms.GMapOverlay("markers");
if (this.markers)
{
map.Overlays.RemoveAt(0);
}
this.markers = true;
GMap.NET.WindowsForms.GMapMarker marker =
new GMap.NET.WindowsForms.Markers.GMarkerGoogle(
new PointLatLng(location.Latitude, location.Langitude),
GMap.NET.WindowsForms.Markers.GMarkerGoogleType.blue_dot);
markers.Markers.Add(marker);
map.Overlays.Add(markers);
}
private void InitializeMap(Location location)
{
map.ShowCenter = false;
map.MapProvider = GMapProviders.GoogleMap;
map.Position = new PointLatLng(location.Latitude, location.Langitude);
map.Zoom = 20;
map.MinZoom = 2;
map.MaxZoom = 40;
map.DragButton = MouseButtons.Left;
}
private double ToLatLng(String text)
{
if (text.Contains("."))
text = text.Replace('.', ',');
return Double.Parse(text);
}
}
}