2013-05-30 26 views
0

首先,我曾与本教程tutorial如何获取单声道的纬度和经度为Android?

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

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

} 

所以,请,如果有人有任何想法有什么错我的代码我会非常感激。

+1

你是什么意思你“无所得”?你没有找到位置提供商吗?它不会产生任何位置结果吗?你有GPS打开/启用?它是否进入了其中一种失败状态(例如,它是否输出“无法确定您的位置”)? –

+0

也许你在模拟器中运行这个,并且地理位置关闭了吗? – sprinter252

+0

感谢您的重播(@Chris Sinclair&sprinter252)我得到的是“无法确定您的位置”,对于GPS,我该如何检查它是否启用? – Mohammadov

回答

0

您的代码中有一处位置正在从后台线程更新UI _addressText.Text。这也可以解释为什么当你点击按钮时,你没有看到任何地址更新。请参阅以下代码的一行代码片段:

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