首页 > 解决方案 > 模拟 GPS 不更新 lon 和 lat

问题描述

好的,我正在使用 应用商店 andriod 中的Lockito来模拟我的位置,我得到了第一个 long 和 lat 但它没有更新,因为我应该将位置存储在列表中

此处的此函数 locationObtained 似乎没有被多次调用。

我正在使用真实设备进行测试,并已授予位置权限。

private async Task<int> StartGps()
{ 

        var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
        if (status != PermissionStatus.Granted)
        {
            if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
            {
                await DisplayAlert("Need location", "Gunna need that location", "OK");                     
            }
            var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
            status = results[Permission.Location];
        }

            ILocation loc = DependencyService.Get<ILocation>();
            loc.locationObtained += (object ss, ILocationEventArgs ee) =>
            {
                lat = ee.lat;
                lng = ee.lng;

                lbllat.Text = ee.lat.ToString();
                lbllong.Text = ee.lng.ToString();
                Position position = new Position(lat, lng);


                postionsList.Add(position);
            };
        listPostions.ItemsSource = postionsList;
        loc.ObtainMyLocation();

       return 1;
}

在我的列表视图中,我有以下内容。

<ListView x:Name="listPostions" > 
<ListView.ItemTemplate>
    <DataTemplate>
     <ViewCell>
        <StackLayout BackgroundColor="#eee"
         Orientation="Vertical">
    <StackLayout Orientation="Horizontal">

     <Label Text="{Binding Latitude}"
                            TextColor="#f35e20" />
     <Label Text="{Binding Longitude}"
                            HorizontalOptions="EndAndExpand"
                            TextColor="#503026" />
                            </StackLayout>
    </StackLayout>
    </ViewCell>
    </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>

问题是它不会自动更新我的应用程序我已将 lockito 设置为开发人员选项中选择的应用程序我有位置并且可以很好地看到 Lockito 中的移动但我看不到来自 Lockito 的其他坐标进入我的应用程序。

这是我的安卓功能

public class GetMyLocation : Java.Lang.Object, ILocation, ILocationListener
{
    public event EventHandler<ILocationEventArgs> locationObtained;

    public void ObtainMyLocation()
    {
        LocationManager lm = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);
        lm.RequestLocationUpdates(LocationManager.NetworkProvider,0, 0, this);

    }

    public void OnLocationChanged(Location location)
    {
        if (location != null)
        {
            LocationEventArgs args = new LocationEventArgs();
            args.lat = location.Latitude;
            args.lng = location.Longitude;
            locationObtained(this, args);
        }
    }

    public void OnProviderDisabled(string provider)
    {
    }

    public void OnProviderEnabled(string provider)
    {
    }

    public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
    {
    }
}

标签: xamarinxamarin.formsxamarin.android

解决方案


推荐阅读