简体   繁体   中英

Xamarin, Android: Why am I getting wrong coordinates?

So I am having this problem - the below code is part of a class in my app. Now, this class gives me coordinates that are ABOUT right to my location. Always a few 100 meters away from where I really should be. Why is this happening? Is this maybe because I dont ask for a "fine" accuracy?

THANKS :)

public void OnLocationChanged(Location location) {

    _currentLocation = location;
    {         
        // this is needed for my mocked location
        GlobalElapsedRealTime = _currentLocation.ElapsedRealtimeNanos;

        GlobalLatitude = _currentLocation.Latitude;
        GlobalLongitude = _currentLocation.Longitude;

       // Log.Debug("2", "Your Real Location is at " + GlobalLongitude + " // " + GlobalLatitude);

    }
}

public void InitializeLocationManager()
{
    _locationManager = ctxt.GetSystemService(Context.LocationService) as LocationManager;

    if (_locationManager.AllProviders.Contains(LocationManager.NetworkProvider)
       && _locationManager.IsProviderEnabled(LocationManager.NetworkProvider))
    {
        _locationProvider = LocationManager.NetworkProvider;
        Log.Debug("1", "Location Manager has been initialized!");
    }
    else
    {
        _locationProvider = String.Empty;
    }
}

public void StartLocationUpdates()
{
    _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);


}

It's because you are using the LocationManager.NetworkProvider . This guesses your location based of cell towers and known WiFi hotspots and is not particularly accurate. Being a few hundred meters out sounds about right. Useful for working out which city or suburb you are in maybe but not for more accurate tracking.

Try using LocationManager.GPSProvider and make sure the ACCESS_FINE_LOCATION permission is enabled. It may take a bit longer to get a fix but it'll be much more accurate. How accurate depends on the device it's run on (phone GPS can be pretty good but not totally accurate as the chips are usually chosen based off price), and how much of a fix you can get - tall buildings can interfere with it for example.

More details:

https://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM