2014-01-06 54 views
2

我正在嘗試在Android.Xamarin中開發商店定位器應用程序。我的第一步是找到我的位置的經緯度。使用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

+0

你應該使用谷歌播放服務的lib .. – Riser

+0

@Segi你可以請提供一個教程或參考鏈接。這是Iam第一次使用Xamarin進行地圖應用程序 –

回答

2

如果您在模擬器中執行此代碼,請記住模擬器不顯示GPS信息。當你在真實設備上面的代碼中嘗試時,確保你的GPS打開,你應該在露天或靠近窗戶的地方。

+0

Thanx的響應。我在一個開放的窗口附近的真實設備上測試了代碼。但結果相同。你也認爲上面的代碼會起作用嗎? –

+0

第一次獲取GPS詳細信息可能需要2-5分鐘。 – Vigbyor

+0

@Vibgyor你是對的..在建築物外面測試後得到座標。 thankx –