How do I get a GPS location update synchronously in Android using Xamarin?
<p>Specifically I am doing C# development using Xamarin.Forms, however working on the native Android side writing a GPS wrapper class which will be usable in the Xamarin.Forms side via dependency injection. The calls should be the same between C# and Java in regards to Android for the most part.</p>

<p>Essentially, I have this method in my Geolocator object (which implements ILocationListener) on the Android side:</p>

<pre><code>public async Task&lt;Tuple&lt;bool, string, GPSData&gt;&gt; GetGPSData() {
        gpsData = null;
        var success = false;
        var error = string.Empty;

        if (!manager.IsProviderEnabled(LocationManager.GpsProvider)) {
            //request permission or location services enabling
            //set error
        } else {
            manager.RequestSingleUpdate(LocationManager.GpsProvider, this, null);
            success = true;
        }

        return new Tuple&lt;bool, string, GPSData&gt;(success, error, gpsData);
 }
</code></pre>

<p>and</p>

<pre><code> public void OnLocationChanged(Location location) {
        gpsData = new GPSData(location.Latitude, location.Longitude);
    }
</code></pre>

<p>I want to be able to call GetGPSData and have it return the tuple, which for now the only important thing about the Tuple is gpsData is filled in. I know it can take seconds to find a fix, so I want this method to be async and be awaitable in the Xamarin.Forms side once I actually need the value.</p>

<p>My issue is I can't figure out a way to have manager.RequestSingleUpdate work synchronously, or any work around. You call that method, and then eventually OnLocationChanged fires off. I tried throwing in a disgusting, barbaric</p>

<pre><code> while (gpsData == null);
</code></pre>

<p>after the call to force it not to continue until OnLocationChanged was fired, however when I put that line in, OnLocationChanged never gets called. I'm assuming that is because OnLocationChanged is invoked on the same thread instead of being a background thread.</p>

<p>Is there any way for me to take this scenario and have GetGPSData not return until OnLocationChanged has fired off?</p>

<p>Thanks</p>

<p>EDIT: To add, this method will not be regularly called. It's spontaneous and rare, so I don't want to use RequestLocationUpdates, get regular updates and return the most recent one because that would require always having GPS on, while would rain batteries unnecessarily.</p>