我正在嘗試在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
我得到的輸出如下所示: -
你應該使用谷歌播放服務的lib .. – Riser
@Segi你可以請提供一個教程或參考鏈接。這是Iam第一次使用Xamarin進行地圖應用程序 –