MAUI:获取当前位置时出现问题

问题描述 投票:0回答:1

我正在使用下面的代码来获取我的 MAUI 应用程序中的当前位置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NeedHelp.Conditional_Compilation
{
    public class GetCurrentLocations
    {
        private CancellationTokenSource _cancelTokenSource;
        private bool _isCheckingLocation;
        public async Task GetCurrentLocation()
        {
            try
            {
                _isCheckingLocation = true;

                GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));

                _cancelTokenSource = new CancellationTokenSource();

                Location location = await Geolocation.Default.GetLocationAsync(request, _cancelTokenSource.Token);

                if (location != null)
                    Preferences.Default.Set("latitude", location.Latitude);
                    Preferences.Default.Set("longitude", location.Longitude);
                Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
            }
            catch (Exception ex)
            {
                // Unable to get location
            }
            finally
            {
                _isCheckingLocation = false;
            }
        }

        public void CancelRequest()
        {
            if (_isCheckingLocation && _cancelTokenSource != null && _cancelTokenSource.IsCancellationRequested == false)
                _cancelTokenSource.Cancel();
        }
    }
}

我将纬度和经度值保存到本地数据库,并在保存到数据库时获取它,如下所示。

GetCurrentLocations getcurrentLocations = new GetCurrentLocations();
await getcurrentLocations.GetCurrentLocation();
string latitude = Preferences.Default.Get("latitude", "");
string longitude = Preferences.Default.Get("longitude", "");
//API call to save the location details

我的问题是用户移动时位置详细信息不准确。当用户移动时,我每 5 秒使用上述 API 将更新的位置详细信息发送到我们的数据库。

保存位置详细信息后,我调用另一个 API 来获取保存的位置,并使用它在地图上画一条线。但画的线不正确,与用户实际走的路径有很大差异。

用户从 A 移动到 B,然后从 B 移动到 C。检查下面的屏幕截图。但地图上显示的线路不一样。

enter image description here

在地图上画线的代码:

if (friendDetails.latitudeList.Count != 0 && friendDetails.longitudeList.Count != 0)
{
    latitude = 0.0;
    longitude = 0.0;
    bool validLocationFound = false;

    for (int i = 0; i < friendDetails.latitudeList.Count; i++)
    {
        if (friendDetails.latitudeList[i] != 0.0 && friendDetails.longitudeList[i] != 0.0)
        {
            latitude = friendDetails.latitudeList[i];
            longitude = friendDetails.longitudeList[i];
            validLocationFound = true;
            break;
        }
    }

    if (validLocationFound)
    {
        Location location = new Location(latitude, longitude);
        map.MoveToRegion(MapSpan.FromCenterAndRadius(new Location(location.Latitude, location.Longitude), Distance.FromMiles(1)));
        Pin pin = new Pin
        {
            Label = "",
            // Address = "The city with a boardwalk",
            Type = PinType.Place,
            Location = new Location(location.Latitude, location.Longitude)
        };
        map.Pins.Add(pin);
        geopin = pin;
    }
    else
    {
        map_layout.IsVisible = false;
    }

    // Determine the number of pairs to add (maximum of 5)
    int count = Math.Min(5, Math.Min(friendDetails.latitudeList.Count, friendDetails.longitudeList.Count));

    // Create a list to hold the valid latitude and longitude pairs
    List<Location> validCoordinates = new List<Location>();

    // Iterate through the latitude and longitude lists to find non-zero coordinates
    int validCount = 0;
    for (int i = 0; i < friendDetails.latitudeList.Count && i < friendDetails.longitudeList.Count; i++)
    {
        if (friendDetails.latitudeList[i] != 0.0 || friendDetails.longitudeList[i] != 0.0)
        {
            validCoordinates.Add(new Location(friendDetails.latitudeList[i], friendDetails.longitudeList[i]));
            validCount++;

            // Stop once we have the required number of valid pairs
            if (validCount == count)
            {
                break;
            }
        }
    }

    // Create the polyline
    Polyline polyline = new Polyline
    {
        StrokeColor = Color.FromArgb("#ea4333"),
        StrokeWidth = 5,
    };

    // Add valid points to the Geopath based on the available coordinates
    foreach (var location in validCoordinates)
    {
        polyline.Geopath.Add(location);
        //fix for the red line is not showing on the map
        map.MapElements.Add(polyline);
    }

    // Now you can use the polyline as needed
    geovalue = polyline;
    map.MapElements.Add(polyline);
    map_layout.IsVisible = true;
}
else
{
    map_layout.IsVisible = false;
}

我尝试了很多,但我不明白这个问题背后的真正原因是什么。位置获取有问题吗?地图上的画线逻辑有问题吗?

maui
1个回答
0
投票

使用

GeolocationAccuracy.High
GeolocationAccuracy.Best
,而不是
GeolocationAccuracy.Medium

© www.soinside.com 2019 - 2024. All rights reserved.