Xamarin Essentials Geolocation Timeout Issue

slink99slink99 Member ✭✭
edited May 31 in Xamarin.Forms

Hi,
I have a multi-platform app which pulls current location and uses that in a loop to calculate the distance to each item. It is using
wineriesView.ItemsSource = await App.WineryManager.GetWineries(seltyp,selval,selwrs);
to load each item which contains latitude and longitude, among other things. It is calling
public Task<List<WineryItem>> GetWineries(string typ, string val, int wrs) { return restService.ReadWineries(typ,val,wrs); }
which then calls
`public async Task<List> ReadWineries(string typ, string val, int wrs)
{

        var position = await Geolocation.GetLocationAsync();
        var uri = new Uri(string.Format(Constants.WineriesUrl, "wineries_by_region.php?region=piedmont"));
        var urio = new Uri(string.Format(Constants.OnlinesUrl, "onlines_by_id.php?id=12"));
        Items = new List<WineryItem>();
        OItems = new List<OnlineItem>();
        if(typ == "Name")
            uri = new Uri(string.Format(Constants.WineriesUrl, "wineries_by_name.php?name=" + val));
        else if(typ == "County")
            uri = new Uri(string.Format(Constants.WineriesUrl, "wineries_by_county.php?county=" + val));
        else if(typ == "Region")
            uri = new Uri(string.Format(Constants.WineriesUrl, "wineries_by_region.php?region=" + val));
        try
        {
            SHA1Managed sha1 = new SHA1Managed();
            var encUname = sha1.ComputeHash(Encoding.UTF8.GetBytes("*********************"));
            var sb = new StringBuilder(encUname.Length * 2);
            foreach (byte b in encUname)
            {
                // can be "x2" if you want lowercase
                sb.Append(b.ToString("x2"));
            }
            string encUsername = sb.ToString();
            var encPwd = sha1.ComputeHash(Encoding.UTF8.GetBytes("*******************"));
            sb = new StringBuilder(encPwd.Length * 2);
            //encUsername = encUsername + ":";
            foreach (byte b in encPwd)
            {
                // can be "x2" if you want lowercase
                sb.Append(b.ToString("x2"));
            }
            //encUsername = encUsername + sb.ToString();
            string encPassword = sb.ToString();
            var bytearray = Encoding.UTF8.GetBytes(encUsername + ":" + encPassword);
            //string encoded = System.Convert.ToBase64String(sb);
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytearray));
            var response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                Records = JsonConvert.DeserializeObject<WineryRecords>(content);
                Items = new List<WineryItem>();
                foreach (var record in Records.records)
                {
                    Location winePosition = new Location(Convert.ToDouble(record.Latitude), Convert.ToDouble(record.Longitude));
                    double dblDistance = Location.CalculateDistance(position, winePosition, DistanceUnits.Miles);
                    if ((wrs == 0) ||
                        (wrs == 2 && record.Type == "Restaurant") ||
                        (wrs == 3 && record.Type == "Shop") ||
                        (wrs == 1 && record.Type == "Winery"))
                    {
                        urio = new Uri(string.Format(Constants.OnlinesUrl, "onlines_by_id.php?id=" + record.Id));
                        cliento.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytearray));
                        var responseo = await client.GetAsync(urio);
                        WineryItem item = new WineryItem();

                        item.Inlink = null;
                        item.Fblink = null;
                        item.Golink = null;
                        item.Trlink = null;
                        item.Twlink = null;
                        item.Welink = null;
                        item.Yelink = null;
                        item.Yolink = null;
                        if (responseo.IsSuccessStatusCode)
                        {
                            content = await responseo.Content.ReadAsStringAsync();

                            Onlines = JsonConvert.DeserializeObject<OnlineRecords>(content);
                            OItems = new List<OnlineItem>();
                            foreach (var online in Onlines.records)
                            {
                                if (online.Description == "facebook")
                                {
                                    item.Fblink = online.Url;
                                    if (online.Url == "")
                                        item.FaIcon = false;
                                    else
                                        item.FaIcon = true;
                                }
                                if (online.Description == "twitter")
                                {
                                    item.Twlink = online.Url;
                                    if (online.Url == "")
                                        item.TwIcon = false;
                                    else
                                        item.TwIcon = true;
                                }
                                if (online.Description == "instagram")
                                {
                                    item.Inlink = online.Url;
                                    if (online.Url == "")
                                        item.InIcon = false;
                                    else
                                        item.InIcon = true;
                                }
                                if (online.Description == "website")
                                {
                                    item.Welink = online.Url;
                                    if (online.Url == "")
                                        item.WeIcon = false;
                                    else
                                        item.WeIcon = true;
                                }
                                if (online.Description == "googlemaps")
                                {
                                    item.Golink = online.Url;
                                    if (online.Url == "")
                                        item.IgIcon = false;
                                    else
                                        item.IgIcon = true;
                                }
                                if (online.Description == "yelp")
                                {
                                    item.Yelink = online.Url;
                                    if (online.Url == "")
                                        item.YeIcon = false;
                                    else
                                        item.YeIcon = true;
                                }
                                if (online.Description == "tripadvisor")
                                {
                                    item.Trlink = online.Url;
                                    if (online.Url == "")
                                        item.TrIcon = false;
                                    else
                                        item.TrIcon = true;
                                }
                                if (online.Description == "youtube")
                                {
                                    item.Yolink = online.Url;
                                    if (online.Url == "")
                                        item.YoIcon = false;
                                    else
                                        item.YoIcon = true;
                                }

                            }
                        }
                        item.Id = record.Id;
                        item.Address = record.Address;
                        item.City = record.City;
                        item.County = record.County;
                        item.Name = record.Name;
                        item.Phone = record.Phone;
                        item.State = record.State;
                        item.Type = record.Type;
                        item.Website = record.Website;
                        item.Zip = record.Zip;
                        item.Tastings = record.Tastings;
                        item.Tours = record.Tours;
                        item.Region = record.Region;
                        if (dblDistance > 0 && dblDistance < 1000)
                        {
                            item.intDistance = Convert.ToInt16(dblDistance);
                            item.strDistance = dblDistance.ToString("N0")+"Mi";
                        } else
                        {
                            item.intDistance = 0;
                            item.strDistance = "--";
                        }
                        Items.Add(item);
                    }
                }

            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"         ERROR {0}", ex.Message);
        }

        return Items;
    }`

With ReadWineries executing all of the code shown, it only returns 10 items to the list, whereas it will return the entire list of 50+ if I comment the geolocation, location, and calculate distance code. How do I extend the timeout for this task so that it will return all items even though it takes longer?

Best Answer

  • slink99slink99 ✭✭
    Accepted Answer

    Hi yelinzh,
    I apologize for so quickly marking your suggestion as not the solution. The statement that geolocation has nothing to do with the issue is true and I discovered that last night. The record that it was blowing on had improperly formatted latitude-longitude data. Thanks for the help.

Answers

  • yousufctecyousufctec USMember ✭✭✭

    Hi @slink99 ,
    With the GeolocationRequest parameter, you will be able to mention the timeout.

    Regards,
    Yousuf.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to specify a timeout for GeolocationRequest like below.

    var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10)); 
    
  • slink99slink99 Member ✭✭

    I have tried the geolocationrequest with timespan, but it still returns only a few items. The code has been modified as shoen below.
    public async Task<List<WineryItem>> ReadWineries(string typ, string val, int wrs) { var request = new GeolocationRequest(GeolocationAccuracy.High); //This has no effect on return of data var position = await Geolocation.GetLocationAsync(request);
    and
    foreach (var record in Records.records) { request = new GeolocationRequest(GeolocationAccuracy.Low,TimeSpan.FromSeconds(10)); Location winePosition = new Location(Convert.ToDouble(record.Latitude), Convert.ToDouble(record.Longitude)); //How do I submit the request timeout here? double dblDistance = Location.CalculateDistance(position, winePosition, DistanceUnits.Miles);
    As commented in the code, how do I submit a timeout to the distance calculation since that seems to be the source of the short data return? It will retrieve all items and take approximately three times as long with the distance calculations commented out, so the timeframe taken by the task itself should not be an issue.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Geolocation has nothing to do with the issue. Check if your code conditional statement filters out part of the content.

    Try to move Items.Add(item); out of the if statement.

  • slink99slink99 Member ✭✭
    Accepted Answer

    Hi yelinzh,
    I apologize for so quickly marking your suggestion as not the solution. The statement that geolocation has nothing to do with the issue is true and I discovered that last night. The record that it was blowing on had improperly formatted latitude-longitude data. Thanks for the help.

Sign In or Register to comment.