Downloads:
Features:
- Get your current location using Google maps
- Get detailed information about your geographic location
- Get current time and date in your location
Notes:
- You need to have a Google API Key and put in source code below, otherwise the application won’t work correctly!
- Application won’t work as expected in case you used proxy!
C# Source Code:
- Main.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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsForms; using GMap.NET.WindowsForms.Markers; using Newtonsoft.Json; using System; using System.Device.Location; using System.Drawing; using System.Globalization; using System.Net; using System.Net.Http; using System.Runtime.InteropServices; using System.Windows.Forms; using static Google_Maps.GoogleLocationInfo; namespace Google_Maps { public partial class Main : Form { [DllImport("wininet.dll")] // Connection Status private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); // Connection Status private GeoCoordinateWatcher Watcher = null; public Main() { InitializeComponent(); } private void btnShowLocation_Click(object sender, EventArgs e) { int Desc; if (InternetGetConnectedState(out Desc, 0) == true) { Watcher = new GeoCoordinateWatcher(); Watcher.StatusChanged += Watcher_StatusChanged; Watcher.Start(); gmap.Zoom = 14; btnShowLocation.Enabled = false; btnResetLocation.Enabled = true; } else { MessageBox.Show("No Internet Connection !"); } } private void btnResetLocation_Click(object sender, EventArgs e) { GeoCoordinate location = Watcher.Position.Location; gmap.Position = new PointLatLng(location.Latitude, location.Longitude); gmap.Zoom = 14; } private void btnGetInfo_Click(object sender, EventArgs e) { int Desc; if (InternetGetConnectedState(out Desc, 0) == true) { Watcher = new GeoCoordinateWatcher(); Watcher.StatusChanged += Watcher_StatusChanged; Watcher.Start(); gmap.Zoom = 14; btnShowLocation.Enabled = false; btnResetLocation.Enabled = true; btnGetInfo.Enabled = false; txtLocationTime.Text = InternetTime(); txtLocationDayName.Text = InternetDay(); } else { MessageBox.Show("No Internet Connection !"); } } private async void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) { if (e.Status == GeoPositionStatus.Ready) { if (Watcher.Position.Location.IsUnknown) { MessageBox.Show("Cannot find location data !"); } else { GeoCoordinate location = Watcher.Position.Location; gmap.MapProvider = GoogleMapProvider.Instance; GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly; GMapProvider.WebProxy = null; GMapOverlay markersOverlay = new GMapOverlay("markers"); GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(location.Latitude, location.Longitude), GMarkerGoogleType.green); markersOverlay.Markers.Add(marker); gmap.Overlays.Add(markersOverlay); marker.ToolTipText = "You are here !"; marker.ToolTip.Fill = Brushes.Black; marker.ToolTip.Foreground = Brushes.White; marker.ToolTip.Stroke = Pens.Black; marker.ToolTip.TextPadding = new Size(20, 20); gmap.DragButton = MouseButtons.Left; gmap.ShowCenter = false; gmap.Position = new PointLatLng(location.Latitude, location.Longitude); btnGetInfo.Enabled = false; this.listBox.Items.Clear(); try { HttpClient client = new HttpClient(); string latlng = location.Latitude + "," + location.Longitude; string APIKey = "Place your API key here!"; string url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={0}&key=" + APIKey; var response = await client.GetAsync(string.Format(url, latlng)); string result = await response.Content.ReadAsStringAsync(); RootObject root = JsonConvert.DeserializeObject<RootObject>(result); txtLocationStatus.Text = root.status; JSONi.Text = result; txtLocationStreetAddress.Text = root.results[0].address_components[1].long_name + " " + root.results[0].address_components[0].long_name; // [ "route" ] + [ "street_number" ] txtLocationCityState.Text = root.results[0].address_components[3].long_name + " / " + root.results[0].address_components[4].long_name; // [ "locality", "political" ] + [ "administrative_area_level_1", "political" ] txtLocationZipCode.Text = root.results[0].address_components[6].long_name; txtLocationCountry.Text = root.results[0].address_components[5].long_name + " - " + root.results[0].address_components[5].short_name; // [ "country", "political" ] txtLocationFullAddress.Text = root.results[0].formatted_address; txtLocationDate.Text = InternetDate(); txtLocationTime.Text = InternetTime(); txtLocationDayName.Text = InternetDay(); marker.ToolTipText = "You are here !\n" + txtLocationStreetAddress.Text + "\n" + txtLocationCityState.Text + "\nZip Code : " + root.results[0].address_components[6].long_name; this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); this.listBox.Items.Add(" > Place ID : " + root.results[0].place_id); this.listBox.Items.Add("**************************** " + " **************************************************************************************************************"); this.listBox.Items.Add(" > Location Type : " + root.results[0].geometry.location_type); this.listBox.Items.Add("**************************** " + " **************************************************************************************************************"); this.listBox.Items.Add(" > Latitude : " + root.results[0].geometry.location.lat); this.listBox.Items.Add("**************************** " + " **************************************************************************************************************"); this.listBox.Items.Add(" > Longitude : " + root.results[0].geometry.location.lng); this.listBox.Items.Add("**************************** " + " **************************************************************************************************************"); this.listBox.Items.Add(" > Exact Latitude : " + Convert.ToString(location.Latitude)); this.listBox.Items.Add("**************************** " + " **************************************************************************************************************"); this.listBox.Items.Add(" > Exact Longitude : " + Convert.ToString(location.Longitude)); this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); this.listBox.Items.Add("*****************************" + "******** Bounds ***********************************************************************************************"); this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); this.listBox.Items.Add(" > Bounds Northeast Lat\t : \t" + root.results[1].geometry.bounds.northeast.lat); this.listBox.Items.Add(" > Bounds Northeast Lng\t : \t" + root.results[1].geometry.bounds.northeast.lng); this.listBox.Items.Add(" > Bounds Southeast Lat\t : \t" + root.results[1].geometry.bounds.southwest.lat); this.listBox.Items.Add(" > Bounds Southeast Lng\t : \t" + root.results[1].geometry.bounds.southwest.lng); this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); this.listBox.Items.Add("*****************************" + "***** ViewPort **************************************************************************************************"); this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); this.listBox.Items.Add(" > Viewport Northeast Lat\t : \t" + root.results[1].geometry.viewport.northeast.lat); this.listBox.Items.Add(" > Viewport Northeast Lng\t : \t" + root.results[1].geometry.viewport.northeast.lng); this.listBox.Items.Add(" > Viewport Southeast Lat\t : \t" + root.results[1].geometry.viewport.southwest.lat); this.listBox.Items.Add(" > Viewport Southeast Lng\t : \t" + root.results[1].geometry.viewport.southwest.lng); this.listBox.Items.Add("*****************************" + "***************************************************************************************************************"); } catch { MessageBox.Show("Some information couldn't be retrieved !\nOr you don't have a valid API Key", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } #region Internet Time public string InternetDate() { var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); var response = myHttpWebRequest.GetResponse(); string todaysDates = response.Headers["date"]; DateTime dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal); string Date = String.Format("{0 : yyyy / MM / dd}", dateTime); response.Close(); return Date; } public string InternetTime() { var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); var response = myHttpWebRequest.GetResponse(); string todaysDates = response.Headers["date"]; DateTime dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal); string Time = String.Format("{0 : HH : mm : ss}", dateTime); response.Close(); return Time; } public string InternetDay() { var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); var response = myHttpWebRequest.GetResponse(); string todaysDates = response.Headers["date"]; DateTime dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal); string Day = Convert.ToString(dateTime.DayOfWeek); response.Close(); return Day; } #endregion } } |
- GoogleLocationInfo.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 |
using System.Collections.Generic; namespace Google_Maps { class GoogleLocationInfo { public class RootObject { public List<Result> results { get; set; } public string status { get; set; } } public class AddressComponent { public List<string> types { get; set; } public string long_name { get; set; } public string short_name { get; set; } } public class Location { public double lat { get; set; } public double lng { get; set; } } public class Northeast { public double lat { get; set; } public double lng { get; set; } } public class Southwest { public double lat { get; set; } public double lng { get; set; } } public class Viewport { public Northeast northeast { get; set; } public Southwest southwest { get; set; } } public class Northeast2 { public double lat { get; set; } public double lng { get; set; } } public class Southwest2 { public double lat { get; set; } public double lng { get; set; } } public class Bounds { public Northeast2 northeast { get; set; } public Southwest2 southwest { get; set; } } public class Geometry { public Location location { get; set; } public string location_type { get; set; } public Viewport viewport { get; set; } public Bounds bounds { get; set; } } public class Result { public List<AddressComponent> address_components { get; set; } public string formatted_address { get; set; } // public Geometry geometry { get; set; } public string place_id { get; set; } // public List<string> types { get; set; } } } } |
Screenshots:
NuGet Packages:
- Newtonsoft.Json
Extra used libraries:
- GMap.NET.Core.dll
- GMap.NET.WindowsForms.dll
- Google.dll
Similar Apps:
Unknown
Contact: