2016-04-08 67 views
0

我想使用Xamarin.Android獲取當前位置和城市名稱。我使用了這個鏈接https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/的代碼,但我只能得到'Latitude'和'Longitude'。也許我沒有得到地址,因爲我正在使用模擬器?獲取當前位置和城市名稱Xamarin.Android

我想解決這個問題,並獲取當前位置和地址使用最少的代碼(使用一些插件,API,也許..)提前:)

回答

0

感謝我覺得你可以去看 geocoder in android

var addresses = await geo.GetFromLocationAsync (42.37419, -71.120639, 1); 

這個配方創建一個地理編碼實例,這是在Android.Locations命名空間。地理編碼器使用經緯度調用GetFromLocationAsync來反轉地理編碼。這將異步執行網絡調用,以便主UI線程不被阻塞。當返回結果時,該方法將返回一個地址列表並在UI線程上繼續。在這個調用中,返回1地址,因爲這是傳遞給GetFromLocationAsync調用的第三個參數的數字。返回的地址包含有關位置的各種信息,包括街道地址。

+0

我使用geolocator插件中一個項目,和其他項目,而不使用這個插件。我正在使用Xamarin Android Player模擬器部署這兩個項目,並且我獲得了經度和緯度,但不是地址..也許我應該安裝Google Play服務或其他內容。在此先感謝 –

0

Xamarin有一篇偉大的技術文章,通過使用Geocoder在Android上完成整個過程。

編號:https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/

,做長/緯度到地址查找該代碼是:

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

FYI:大多數模擬器有嘲諷LatitudeLongitude用戶自定義設置,以便您可以更改它並測試獲得不同的迴應。

即在Xamarin的Android播放器,還有的GPS設置的對話框:

enter image description here

有在GenyMotion的Android模擬器,等等類似的設置......

+0

我在一個項目中使用geolocator插件,而其他項目不使用此插件。我正在使用Xamarin Android Player模擬器部署這兩個項目,並且我獲得了經度和緯度,但不是地址..也許我應該安裝Google Play服務或其他內容。提前致謝 –

相關問題