xamarin android无效的提供程序:null

时间:2018-10-19 18:04:56

标签: xamarin.android

运行后的

app返回位置管理器的空值。 请帮我解决。 该地图还可以,但是位置更改侦听器返回null值以获取最佳提供程序。

我的问题是在这一行中重新调整null值:

  
    

string locationProvider = _locMgr.GetBestProvider(locationCriteria,false);

  

这是代码

using Android.App;
using Android.Support.V7.App;
using MapboxAccountManager = Com.Mapbox.Mapboxsdk.Mapbox;
using Com.Mapbox.Mapboxsdk.Maps;
using Com.Mapbox.Mapboxsdk.Camera;
using Com.Mapbox.Mapboxsdk.Geometry;
using Com.Mapbox.Mapboxsdk.Annotations;
using Android.Locations;

namespace App4
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", 
MainLauncher = true)]
    public class MainActivity : AppCompatActivity, IOnMapReadyCallback, 
ILocationListener
    {
        MapView mapView;
        MapboxMap mMap;
        LocationManager _locMgr;

        public void OnMapReady(MapboxMap map)
        {
            var position = new CameraPosition.Builder()
               .Target(new LatLng(35, 53))
               .Zoom(11)
               .Build();               map.AnimateCamera(CameraUpdateFactory.NewCameraPosition(position));
        mMap = map;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        MapboxAccountManager.GetInstance(this, "pk.eyJ1IjoiamFsYWxvbmFnaCIsImEiOiJjajBvOGhjdDQwMDFhMzNudjNjb3c3MW43In0.8jT28rJpuwO6z6U6GRXrWA");
        SetContentView(Resource.Layout.activity_main);

        mapView = FindViewById<MapView>(Resource.Id.mapView);
        //mapView.StyleUrl = Style.MapboxStreets;
        mapView.OnCreate(savedInstanceState);

        mapView.GetMapAsync(this);

        gpsSetup();
    }

    public void OnLocationChanged(Android.Locations.Location location)
    {
        var position = new CameraPosition.Builder()
            .Target(new LatLng(location.Latitude, location.Longitude))
            .Zoom(11)
            .Build();

        mMap.AnimateCamera(CameraUpdateFactory.NewCameraPosition(position));
    }

    public void gpsSetup()
    {
        try
        {
            _locMgr = GetSystemService(LocationService) as LocationManager;
            var locationCriteria = new Criteria
            {
                Accuracy = Accuracy.Fine,
                PowerRequirement = Power.Medium
            };
            string locationProvider = _locMgr.GetBestProvider(locationCriteria, false);
            _locMgr.RequestLocationUpdates(locationProvider, 2000, 2, this);
        }
        catch (Exception Ex)
        {
            Toast.MakeText(this, Ex.Message, ToastLength.Long).Show();
        }
    }
}

}

0 个答案:

没有答案