android - 使用 Android.Xamarin 查找纬度和经度

标签 android google-maps xamarin.ios gps

我正在尝试在 Android.Xamarin 中开发商店定位器应用程序。我的第一步是找到我所在位置的纬度和经度。

但是我的模拟器/设备屏幕什么也没显示。

我有我的 uses-permissions设置为 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

这是我的代码:-

Location _currentLocation;
    LocationManager _locationManager;
    TextView _locationText;
    TextView _addressText;
    string _locationProvider;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
        _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();
    }

    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;
        }
    }

    protected override void OnResume ()
    {
        base.OnResume ();
        _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
    }

    protected override void OnPause ()
    {
        base.OnPause ();            
        _locationManager.RemoveUpdates(this);
    }


    async void AddressButton_OnClick(object sender, EventArgs eventArgs)
    {
        if (_currentLocation == null)
        {
            _addressText.Text = "Can't determine the current address.";
            return;
        }

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

        Address address = addressList.FirstOrDefault();
        if (address != null)
        {
            StringBuilder deviceAddress = new StringBuilder();
            for (int i = 0; i < address.MaxAddressLineIndex; i++)
            {
                deviceAddress.Append(address.GetAddressLine(i))
                    .AppendLine(",");
            }
            _addressText.Text = deviceAddress.ToString();
        }
        else
        {
            _addressText.Text = "Unable to determine the address.";
        }
    }
    #region ILocationListener implementation
    public void OnLocationChanged (Location location)
    {
        _currentLocation = location;
        if (_currentLocation == null)
        {
            _locationText.Text = "Unable to determine your location.";
        }
        else
        {
            _locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude);
        }
    }

    public void OnProviderDisabled (string provider)
    {       
    }

    public void OnProviderEnabled (string provider)
    {       
    }

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

我得到如下输出:-

enter image description here

enter image description here

最佳答案

如果您在模拟器中执行此代码,请记住模拟器不显示 GPS 信息。当您在真实设备中尝试上述代码时,请确保您的 GPS 已打开并且您应该在开阔的天空下方或靠近 window 。

关于android - 使用 Android.Xamarin 查找纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20946293/

相关文章:

xamarin.ios - 为什么我必须保留对 UIViewController 的引用以防止它被垃圾收集?

c# - 我不能将 null 作为标题传递给 MonoTouch 中的 UIActionSheet 构造函数有什么原因吗?

ios - UIRefreshControl 在 Xamarin iOS 上崩溃

android - 将图像从相机加载到 Fragment 中的 ImageView

即使在 build.gradle 文件中添加了 Android Studio 3.0 也无法导入 Google Play 服务

html - 如何在 Bootstrap 模式中添加谷歌地图

java - 我无法从用于 Android 的 Google Maps API 获取准确的 GPS 位置

android - FFmpeg 错误 : Unable to find a suitable output format for ''

Android ime 选项不起作用

android - 如何在 Linux mint 中更新 Android Studio?错误: Studio does not have write access to/app/extra. 请以特权用户运行更新