using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Locations; using Android.Util; using System.Timers; namespace CommonLib { public class LocationInfo : Activity, ILocationListener { protected Location currentLocation = null; protected LocationManager locationManager = null; string locationProvider = string.Empty; DateTime locationReceiveTime = DateTime.Now; public void InitLocationInfo() { try { CheckGpsService(); this.locationManager = (LocationManager)GetSystemService(LocationService); RequestLocationUpdates(); } catch { } } public void OnLocationChanged(Location location) { try { currentLocation = location; OnLocationChanged(); } catch { } } protected virtual void OnLocationChanged() { this.locationReceiveTime = DateTime.Now; } public Boolean GetGeoLocation(ref double dLatPoint, ref double dLongitude) { if (currentLocation == null) return false; dLatPoint = currentLocation.Latitude; dLongitude = currentLocation.Longitude; return true; } public void RequestLocationUpdates() { try { if (this.locationManager.IsProviderEnabled(LocationManager.GpsProvider) == true) { // GPS·Î ºÎÅÍ À§Ä¡ Á¤º¸¸¦ ¾÷µ¥ÀÌÆ® ¿äû this.locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 0, 0, this); } else { } // ±âÁö±¹À¸·ÎºÎÅÍ À§Ä¡ Á¤º¸¸¦ ¾÷µ¥ÀÌÆ® ¿äû this.locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 1000, 0, this); } catch(Exception) { } } public double GetDistance(double lat1, double lon1, double lat2, double lon2) { double theta, dist; theta = lon1 - lon2; dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)); dist = Math.Acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.1515; dist = dist * 1.609344; // ´ÜÀ§ mile ¿¡¼­ km º¯È¯. dist = dist * 1000.0; // ´ÜÀ§ km ¿¡¼­ m ·Î º¯È¯ return dist; } // ÁÖ¾îÁø µµ(degree) °ªÀ» ¶óµð¾ðÀ¸·Î º¯È¯ private double deg2rad(double deg) { return (double)(deg * Math.PI / (double)180d); } // ÁÖ¾îÁø ¶óµð¾ð(radian) °ªÀ» µµ(degree) °ªÀ¸·Î º¯È¯ private double rad2deg(double rad) { return (double)(rad * (double)180d / Math.PI); } protected override void OnResume() { base.OnResume(); } protected override void OnPause() { base.OnPause(); } public void OnStatusChanged(string provider, Availability status, Bundle extras) { } public void OnProviderDisabled(string provider) { } public void OnProviderEnabled(string provider) { } private bool CheckGpsService() { String strGPS = Android.Provider.Settings.Secure.GetString(ContentResolver, Android.Provider.Settings.Secure.LocationProvidersAllowed); int nPos = strGPS.IndexOf("gps"); if (nPos < 0) { MessageBox messageBox = MessageBox.Execute(this, "À§Ä¡¼­ºñ½º ¼³Á¤", "¹«¼± ³×Æ®¿öÅ© »ç¿ë, GPS À§¼º »ç¿ëÀ» ¸ðµÎ üũÇÏ¼Å¾ß Á¤È®ÇÑ À§Ä¡ ¼­ºñ½º°¡ °¡´ÉÇÕ´Ï´Ù.\nÀ§Ä¡ ¼­ºñ½º ±â´ÉÀ» ¼³Á¤ÇϽðڽÀ´Ï±î?", "È®ÀÎ"); messageBox.okEvent += OnMessageBoxOK; return false; } return true; } void OnMessageBoxOK() { Intent intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings); intent.AddCategory(Intent.CategoryDefault); StartActivity(intent); } } }