【问题标题】:Collecting GPS co-Ordinates收集 GPS 坐标
【发布时间】:2016-04-08 15:54:08
【问题描述】:

我正在使用 xamarin 构建本机 android 应用程序。问题是,该应用程序在模拟器上完美地收集和显示坐标,但是当我将它放在智能手机上(尝试了 2 部三星手机)时,它无法确定当前地址。额外的信息数据和位置已打开,所以我不确定问题出在哪里。谢谢你的帮助。这是帮助https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/

的xammarin配方包装
    [Activity(Label = "NewRoute")]
    public class NewRouteActivity : Activity, ILocationListener
    {

        static readonly string TAG = "X:" + typeof(NewRouteActivity).Name;
        TextView _addressText;
        Location _currentLocation;
        LocationManager _locationManager;

        string _locationProvider;
        TextView _locationText;


        public async void OnLocationChanged(Location location) {
            _currentLocation = location;
            if (_currentLocation == null)
            {
                _locationText.Text = "Unable to determine your location. Try again in a short while.";
            }
            else
            {
                _locationText.Text = string.Format("{0:f6},{1:f6}", _currentLocation.Latitude, _currentLocation.Longitude);
                Address address = await ReverseGeocodeCurrentLocation();
                DisplayAddress(address);
            }
        }

        public void OnProviderDisabled(string provider) { }

        public void OnProviderEnabled(string provider) { }

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

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.CreatetRoute);

            _addressText = FindViewById<TextView>(Resource.Id.address_text);
            _locationText = FindViewById<TextView>(Resource.Id.location_text);
            FindViewById<TextView>(Resource.Id.get_address_button).Click += AddressButton_OnClick;

            InitializeLocationManager();


            Button btnEndPoint = FindViewById<Button>(Resource.Id.btnEndPoint);
            btnEndPoint.Click += new EventHandler(AfterPointsCollected);
        }
        //Location Stuff
        void InitializeLocationManager()
        {
            _locationManager = (LocationManager)GetSystemService(LocationService);
            Criteria criteriaForLocationService = new Criteria
            {
                Accuracy = Accuracy.Fine
            };
            IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

            if (acceptableLocationProviders.Any())
            {
                _locationProvider = acceptableLocationProviders.First();
            }
            else
            {
                _locationProvider = string.Empty;
            }
            Log.Debug(TAG, "Using " + _locationProvider + ".");
        }
        //Override OnResume so that Activity1 will begin listening to the LocationManager when the activity comes into the foreground:
        protected override void OnResume()
        {
            base.OnResume();
            _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
        }
        async void AddressButton_OnClick(object sender, EventArgs eventArgs)
        {
            if (_currentLocation == null)
            {
                _addressText.Text = "Can't determine the current address. Try again in a few minutes.";
                return;
            }

            Address address = await ReverseGeocodeCurrentLocation();
            DisplayAddress(address);
        }

        async Task<Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder geocoder = new Geocoder(this);
            IList<Address> addressList =
                await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();
            return address;
        }

        void DisplayAddress(Address address)
        {
            if (address != null)
            {
                StringBuilder deviceAddress = new StringBuilder();
                for (int i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceAddress.AppendLine(address.GetAddressLine(i));
                }
                // Remove the last comma from the end of the address.
                _addressText.Text = deviceAddress.ToString();
            }
            else
            {
                _addressText.Text = "Unable to determine the address. Try again in a few minutes.";
            }
        }

        //Override OnPause and unsubscribe Activity1 from the LocationManager when the activity goes into the background:
        protected override void OnPause()
        {
            base.OnPause();
            _locationManager.RemoveUpdates(this);
        }
        //Changing Activity
        void AfterPointsCollected(object sender, EventArgs e)
        {
            //context   //activity
            Intent intent = new Intent(this, typeof(AfterPointsCollectedActivity));
            //starts the activity with the intent above
            this.StartActivity(intent);

        }

【问题讨论】:

  • 您能否更具体地说明您的错误消息是什么。提供您的 LogCat 或屏幕截图。您是否尝试过对此进行调试以确定问题发生在代码中的哪个位置。
  • 问题是没有错误它在模拟器上完美运行,但在移动设备上编译时它只是不起作用它只是说所有东西都是空的,就好像它没有得到 gps即使位置和数据都在坐标上,这也是我弹出错误消息的原因。
  • 您是否尝试在设备上使用其他应用程序来获取位置,例如 Google 地图。确保您的设备正确获取这些应用的位置。
  • 是的,谷歌地图在这两种设备上都可以使用。在我的应用程序失败后我尝试了检查。
  • 是否有多个 Fine LocationProvider 可用?您是否尝试过使用 First() 以外的其他方法?您是否在清单中启用了精细位置权限?

标签: c# android google-maps xamarin gps


【解决方案1】:

您的手机可能正在运行 MarshMallow,现在需要您请求位置服务权限。

您可以在此处https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/ 阅读更多相关信息。你可能想使用这个 Nuget 包来为你处理所有这些。 https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Geolocator

【讨论】:

    猜你喜欢
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多