Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Geolocation - How can I improve the retrieval of coordinates ?

betcssbetcss Member ✭✭
edited November 2019 in Xamarin.Forms

How may I improve on this:

 private static Location loc = null;
 private async void BttLocation_Clicked(object sender, EventArgs e)
        {
            if (loc != null)
            {
                loc = await Geolocation.GetLastKnownLocationAsync();
            }
            else
            {
                var request = new GeolocationRequest(GeolocationAccuracy.Medium);
                loc = await Geolocation.GetLocationAsync(request);
            }

            Map.MoveToMapRegion(MapSpan.FromCenterAndRadius(
                    new Position(loc.Latitude, loc.Longitude), Distance.FromMiles(1)), true);
        }

Best Answers

  • betcssbetcss Member ✭✭
    Accepted Answer

    In other words, what I've already done is good enough or ...

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    How can I improve the retrieval of coordinates

    What feature do you want? The more accurate or faster? Please post more details about the improvement.

    We can get the last known location of the device by calling the GetLastKnownLocationAsync method which is often faster then doing a full query, but can be less accurate. If you want to improve Geolocation accuracy, set the value to Best.

    var request = new GeolocationRequest(GeolocationAccuracy.Best);
    var location = await Geolocation.GetLocationAsync(request);
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android

  • betcssbetcss Member ✭✭
    edited November 2019

    What I'm interested is if the code above can be improved upon. E.g. Could the initial retrieval be faster than in the way I've done it above, with the static variable ?

  • JarvanJarvan Member, Xamarin Team Xamurai

    Could the initial retrieval be faster than in the way I've done it above, with the static variable ?

    Create an instance of Location and then pass the value to it. This'll optimize the code but a little. The if (loc != null) statement as above works better because GetLastKnownLocationAsync get location of the device faster.

  • betcssbetcss Member ✭✭
    Accepted Answer

    In other words, what I've already done is good enough or ...

  • JarvanJarvan Member, Xamarin Team Xamurai

    It's unnecessary to optimize the code. You can compare the effect with removing static variables.

  • thisismyselfthisismyself Member ✭✭
                var request = new GeolocationRequest(GeolocationAccuracy.Medium);
                loc = await Geolocation.GetLocationAsync(request);
    

    Be aware this code may take a couple of seconds to return. Consider running it in a dedicated Task on ThreadPool or something a similar and return the value for example with System.Reactive to observe it and react to it on the UI thread.

  • betcssbetcss Member ✭✭
    edited November 2019

    @thisismyself
    Can you elaborate with an example or links, since that is what I was asking in a first place?

Sign In or Register to comment.