Ahmed Salman Tahir
Ahmed Salman Tahir

Reputation: 1779

Get Current Location Instantly

I am using the following code to request current location:

private void RequestCurrentLocation()
{
    Criteria locationCriteria = new Criteria () { Accuracy = Accuracy.NoRequirement, PowerRequirement = Power.NoRequirement };

    this.mgr = GetSystemService (Context.LocationService) as LocationManager;
    String locationProvider = this.mgr.GetBestProvider (locationCriteria, true);
    this.mgr.RequestLocationUpdates (locationProvider, 0, 0, this);
}

As I need location only once, so I call RemoveUpdates() as soon as OnLocationChanged is raised and then carry on with rest of the working:

public void OnLocationChanged (Location location)
{
    this.mgr.RemoveUpdates (this);

    //Rest of the method
}

I face two issues:

1) Although I have provided zero in the distance and time parameters of RequestLocationUpdates but it still needs at least 2-3 seconds before the OnLocationChanged is triggered. How can I make it instantaneous?

2) I intermittently face the issue that the OnLocationChanged event does not fire at all. Yesterday I spent the whole day to get my code working which was flawlessly working a day earlier. It's really strange that something works properly one day and the next day it simply stops even with the very same source code! Can somebody give me an idea?

Thanks.

Upvotes: 0

Views: 145

Answers (1)

Budius
Budius

Reputation: 39836

There's no way to get an accurate, up-to-date location instantly.

For a device to determine location it needs to track GPS, WiFi, 3G towers and that depends on radio signal strength, transmission speed and other little stuff like that. You can trick the parameters of your request to try to have a faster update (for example, instead of best provider, try to use any provider available).

As a alternative approach you can request to getLastKnownLocation, this method is synchronous and replies instantly with the last location that the system found. The issue is that the "last known location" might be null (if the system never found any location), or might be very old (if the last location was days ago).

Upvotes: 2

Related Questions