我是xamarin Android的新手....我有一个应用程序,显示用户当前的纬度和经度似乎正在工作...
现在,从纬度和经度来看,我试图使用API Geocoder获取地址。我正在经过适当的纬度和经度,但它没有给我任何地址,虽然我看不出任何错误。
以下是我的代码: -
async Task<Address> ReverseGeocodeCurrentLocationAsync(double Latitude, double Longitude)
{
Geocoder geocoder = new Geocoder(this);
IList<Address> addressList = await
geocoder.GetFromLocationAsync(Latitude, Longitude, 10); // ???????? its not wrking....Here, Im properly able to pass latitude and longitude still issue getting adress.
IList<Address> testaddresses = await geocoder.GetFromLocationAsync(42.37419, -71.120639, 1); // ???????? i tried both sync and async method but not wrking....
Address address = addressList.FirstOrDefault();
return address;
}
//调用部分 地址address = await ReverseGeocodeCurrentLocationAsync(location.Latitude,location.Longitude);
此外,您可以在https://github.com/4pawan/XamarinDroidSample
找到源代码请让我知道我做错了什么以及如何纠正?
此致 爬完
答案 0 :(得分:1)
始终检查Geocoder
是否可用,因为这需要后台服务可用,并且它可能不可用,因为它不包含在基本Android框架中:
Geocoder.IsPresent
在Google的Dev API控制台中注册您的应用
将您的Google地图API API添加到您应用的清单
如果您使用Google的融合位置提供商(通过Google Play服务)并需要地理位置编码,请确保您的应用具有ACCESS_FINE_LOCATION
权限:
从Geocoder
服务接收地址列表需要网络连接。
根据您设备的Geocoder
服务,在“Google”或您设备的Geocoder服务回复地址列表之前,可能需要多一个请求。
注意:非常频繁的回答是:
`Timed out waiting for response from server`
在这种情况下,请等待一段时间,然后重试。
但是还有许多其他错误,包括找不到地址,无效的lat / log,当前没有的地理编码器等等......
注意:我通常使用ReactiveUI
来包装失败,重试和延续,但这是一个简单的例子:
基本地理编码方法(非常类似):
async Task<Address> ReverseGeocodeLocationAsync(Location location)
{
try
{
var geocoder = new Geocoder(this);
IList<Address> addressList = await geocoder.GetFromLocationAsync(location.Latitude, location.Longitude, 3);
Address address = addressList.FirstOrDefault();
return address;
}
catch (Exception e)
{
Log.Error(TAG, e.Message);
}
return null;
}
重试:
int retry = 0;
Address address = null;
do
{
address = await ReverseGeocodeLocationAsync(_currentLocation);
if (address != null)
{
Log.Info(TAG, $"Address found: {address.ToString()}");
// Do something with the address(es)
break;
}
retry++;
Log.Warn(TAG, $"No addresses returned...., retrying in {retry * 2} secs");
await Task.Delay(retry * 2000);
} while (retry < 10);
答案 1 :(得分:0)
这是在Xmarin android中使用纬度和经度获取完整地址的方式
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Xamarin.Essentials;
using System;
using System.Threading.Tasks;
using Android.Content.PM;
using System.Collections.Generic;
using System.Linq;
namespace GeoLocation
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView txtnumber;
protected async override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
txtnumber = FindViewById<TextView>(Resource.Id.textView1);
double GmapLat = 0;
double GmapLong = 0;
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
var location = await Geolocation.GetLocationAsync(request);
txtnumber.Text = "finish get geolocation";
GmapLat = location.Latitude;
GmapLat=location.Longitude;
if (location != null)
{
var placemarks = await Geocoding.GetPlacemarksAsync(location.Latitude, location.Longitude);
var placemark = placemarks?.FirstOrDefault();
if (placemark != null)
{
// Combine those string to built full address... placemark.AdminArea ,placemark.CountryCode , placemark.Locality , placemark.SubAdminArea , placemark.SubLocality , placemark.PostalCode
string GeoCountryName = placemark.CountryName;
}
txtnumber.Text = "GPS: Latitude " + location.Latitude + " Longitude " + location.Longitude;
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
txtnumber.Text = ex.Message.ToString();
}
}
}
}