Search code examples
androidxamarinxamarin.androidandroid-gps

Get GPS location even GPS turn off with XAMARIN


I use XAMARIN to Android.

How can I get GPS location even GPS is turn off?


Solution

  • You can get the approximate location by getting ip.

    Here is the interface code:

    public interface IDeviceOrientationService
    {
        DeviceOrientation GetOrientation();
    }
    

    Here is the code implemented in Android:

    [assembly: Dependency(typeof(IPAddressManager))]
    namespace App9.Droid
    {
        class IPAddressManager: IIPAddressManager
        {
            public string GetIPAddress()
            {
                IPAddress[] adresses = Dns.GetHostAddresses(Dns.GetHostName());
                if (adresses != null && adresses[0] != null)
                {
                    return adresses[0].ToString();
                }
                else
                {
                return null;
                }
            }
        }
    }
    

    Here is the calling code:

    string myAddress = DependencyService.Get<IIPAddressManager>().GetIPAddress();