发现经纬度使用Android.Xamarin [英] Finding latitude and longitude using Android.Xamarin
问题描述
我试图发展Android.Xamarin商店定位器应用程序。我的第一步是要找到我的位置的经度和纬度。
I am trying to develop a store locator app in Android.Xamarin. My first step is to find my location's latitude and longitude.
但我的模拟器/设备屏幕显示什么。
But my emulator/device screen shows nothing.
我有我的使用的权限
设置为<使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
<使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
这是我的code: -
This is my code:-
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
我得到的输出如下所示: -
I get the output as the following:-
推荐答案
如果您在执行此模拟器code,那么记住这一点是仿真器不显示GPS信息。而当你在上面实时设备code试图,请确保您的GPS开启时,你应低于开放天空或接近窗口。
If you are executing this code in emulator then keep this in mind that Emulator doesn't display GPS Information. And when you are trying in above code in real device, make sure, your GPS is turn on and you should be below open sky or near Window.
这篇关于发现经纬度使用Android.Xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!