Real Time Map Update

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

我正在使用google maps和android的Locations服务,即使我在移动设备时该方法也不会更新ui。我已请求允许使用我在移动设备时会假定的位置]

locationObtained将在用户移动设备时获得lat和很长时间的更新信息,但这似乎只获得了一次。

double lat=0.00;
double lng=0.00;
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];
}
if (status == PermissionStatus.Granted)
{
    ILocation loc = DependencyService.Get<ILocation>();
    loc.locationObtained += (object ss, ILocationEventArgs ee) =>
    {
         loc.ObtainMyLocation();
         lat = ee.lat;
         lng = ee.lng;

         lbllat.Text = ee.lat.ToString();
         lbllong.Text = ee.lng.ToString();

    };
        Position position = new Position(lat, lng);
        map.Pins.Add(_pinTokyo2);    

    }
}

这是我使用依赖项服务获取位置的类。

namespace Fitnessbudy.Droid
{
    public class LocationEventArgs : EventArgs, ILocationEventArgs
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    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)
        {
        }
    }
 }

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.fitnessbudy" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="Fitnessbudy.Android"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
</manifest>

我确保我处于发布模式。这样就授予了权限。

xamarin xamarin.forms xamarin.android
1个回答
0
投票

您对loc.ObtainMyLocation的呼叫位于回调内部,因此不会初始化您的位置更新。

~~~
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();

};
loc.ObtainMyLocation();
~~~
© www.soinside.com 2019 - 2024. All rights reserved.