Xamarin android基于纬度和经度获取地址

Xamarin android基于纬度和经度获取地址,android,xamarin,geocode,Android,Xamarin,Geocode,我是xamarin Android的新用户…我有一个应用程序,显示用户当前的纬度和经度,似乎正在工作 现在,从纬度和经度,我尝试使用API地理编码器获取地址。我经过适当的纬度和经度,但它没有给我任何地址,虽然我看不到任何错误 以下是我的代码:- async Task<Address> ReverseGeocodeCurrentLocationAsync(double Latitude, double Longitude) { Geocoder

我是xamarin Android的新用户…我有一个应用程序,显示用户当前的纬度和经度,似乎正在工作

现在,从纬度和经度,我尝试使用API地理编码器获取地址。我经过适当的纬度和经度,但它没有给我任何地址,虽然我看不到任何错误

以下是我的代码:-

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;
        }
异步任务

请让我知道我做错了什么以及如何纠正

问候,, 帕万
  • 始终检查
    Geocoder
    是否可用,因为这需要提供后台服务,并且可能不可用,因为它未包含在基本Android框架中:

    Geocoder.IsPresent

  • 在Google的开发API控制台中注册你的应用程序

  • 在应用程序清单中添加Google Map API密钥

  • 如果您正在使用谷歌的融合定位服务提供商(通过谷歌Play服务)并需要地理编码,请确保您的应用程序具有
    ACCESS\u FINE\u location
    权限:

  • 需要网络连接才能从
    Geocoder
    服务接收地址列表

  • 根据您设备的
    Geocoder
    服务,在“谷歌”或您设备的Geocoder服务回复地址列表之前,可能需要多个请求

    • 不要向服务发送垃圾邮件,您将受到限制,在请求之间使用越来越大的时间延迟
  • 注:一个非常频繁的响应是:

    `Timed out waiting for response from server`
    
    在这种情况下,请稍等片刻,然后重试

    但还有许多其他错误,包括未找到地址、lat/log无效、地理编码器当前不可用等

    注意:我通常使用
    ReactiveUI
    包装失败、重试和继续,但下面是一个简单的示例:

    基本地理编码方法(与您的方法非常相似):

    async Task ReverseGeocodeLocationAsync(位置)
    {
    尝试
    {
    var geocoder=新的地理编码器(本);
    IList addressList=await geocoder.GetFromLocationAsync(location.Latitude,location.Longitude,3);
    Address=addressList.FirstOrDefault();
    回信地址;
    }
    捕获(例外e)
    {
    日志错误(标签,例如消息);
    }
    返回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);
    
    int重试=0;
    地址=空;
    做
    {
    地址=等待反向定位同步(_currentLocation);
    if(地址!=null)
    {
    Info(标记,$”找到的地址:{Address.ToString()}”);
    //对地址做些什么
    打破
    }
    重试++;
    Log.Warn(标记,$“未返回地址…”,在{retry*2}秒内重试);
    等待任务。延迟(重试*2000);
    }而(重试<10次);
    
    这是在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();
                }
    
    
    
            }
    
    
    
        }
    
    
    
    }
    
    使用Android.App;
    使用Android.OS;
    使用Android.Support.V7.App;
    使用Android.Runtime;
    使用Android.Widget;
    使用Xamarin.Essentials;
    使用制度;
    使用System.Threading.Tasks;
    使用Android.Content.PM;
    使用System.Collections.Generic;
    使用System.Linq;
    命名空间地理定位
    {
    [活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
    公共类MainActivity:AppCompativeActivity
    {
    TextView txtnumber;
    创建时受保护的异步重写void(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(这个,savedInstanceState);
    全局::Xamarin.Forms.Forms.Init(这个,savedInstanceState);
    //从“主”布局资源设置视图
    SetContentView(Resource.Layout.activity_main);
    txtnumber=findviewbyd(Resource.Id.textView1);
    双GmapLat=0;
    双GmapLong=0;
    尝试
    {
    var请求=新地理定位请求(geolocationaccurity.Medium,TimeSpan.FromSeconds(10));
    var location=await Geolocation.GetLocationAsync(请求);
    txtnumber.Text=“完成获取地理位置”;
    GmapLat=位置、纬度;
    GmapLat=位置经度;
    如果(位置!=null)
    {
    var placemarks=await Geocoding.GetPlacemarksAsync(location.Latitude,location.Longitude);
    var placemark=placemarks?.FirstOrDefault();
    if(placemark!=null)
    {
    //将这些字符串组合到生成的完整地址…placemark.AdminArea、placemark.CountryCode、placemark.Locality、placemark.SubAdminArea、placemark.SubLocality、placemark.PostalCode
    字符串GeoCountryName=placemark.CountryName;
    }
    txtnumber.Text=“GPS:纬度”+位置.纬度+“经度”+位置.经度;
    }
    }
    捕获(功能不支持异常fnsEx)
    {
    //设备异常不支持句柄
    }
    捕获(FeatureNotEnabledException fneEx)
    {
    //设备异常时未启用句柄
    }
    捕获(许可异常pEx)
    {
    //处理权限异常
    }
    捕获(例外情况除外)
    {
    //无法获取位置
    txtnumber.Text=ex.Message.ToString();
    }
    }
    }
    }
    
    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();
                }
    
    
    
            }
    
    
    
        }
    
    
    
    }