【问题标题】:How to get latitude and longitude in Mono for android?如何在Mono for android中获取纬度和经度?
【发布时间】:2013-05-30 12:05:38
【问题描述】:

首先我使用本教程tutorial

获取纬度和经度,但我什么也没得到,所以这是我的代码:

[Activity(Label = "GetLocation", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity, ILocationListener
{
    private Location _currentLocation;
    private LocationManager _locationManager;
    private TextView _locationText;
    private TextView _addressText;
    private 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();
    }

    private void InitializeLocationManager()
    {
        _locationManager = (LocationManager)GetSystemService(LocationService);
        var criteriaForLocationService = new Criteria
        {
            Accuracy = Accuracy.Fine
        };
        var 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);
    }

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

        new Thread(() =>
        {
            var addressText = "Unable to find a location.";
            var geocoder = new Geocoder(this);
            var addressList = geocoder.GetFromLocation(_currentLocation.Latitude, _currentLocation.Longitude, 50);
            var address = addressList.FirstOrDefault();

            if (address != null)
            {
                var deviceLocation = new StringBuilder();
                for (var i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceLocation.Append(address.GetAddressLine(i))
                        .AppendLine(",");
                }
                _addressText.Text = deviceLocation.ToString();
            }
            RunOnUiThread(() => { _addressText.Text = addressText; });
        }).Start();
    }

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

}

如果有人知道我的代码有什么问题,我将不胜感激。

【问题讨论】:

  • 你“一无所获”是什么意思?您没有找到位置提供商吗?它不会产生任何位置结果吗?您是否打开/启用了 GPS?它是否进入您的失败状态之一(例如,它是否输出“无法确定您的位置”)?
  • 也许你是在模拟器中运行这个并且那里的地理位置被关闭了?
  • 感谢您的重播(@Chris Sinclair 和 sprinter252)我得到的是“无法确定您的位置”,对于 GPS,我如何检查它是否已启用?跨度>
  • @Mohammadov 这可能就是为什么。 Google 上有一些资源可以查看,以便模拟 GPS 位置。默认情况下,我认为模拟器已关闭 GPS 并且(大部分)没有功能(毕竟,它实际上没有物理 GPS 可供使用)

标签: c# xamarin.android xamarin


【解决方案1】:

您的代码中有一个地方是您从后台线程更新 UI _addressText.Text。这也可以解释为什么当您单击按钮时,您没有看到任何地址更新。一行代码的 sn-p 如下:

if (address != null)
{
    var deviceLocation = new StringBuilder();
    for (var i = 0; i < address.MaxAddressLineIndex; i++)
    {
        deviceLocation.Append(address.GetAddressLine(i))
            .AppendLine(",");
    }

    // Here you were updating the UI thread from the background:
    RunOnUiThread(() => _addressText.Text = deviceLocation.ToString());
}

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 2012-06-30
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多