【问题标题】:Xamarin android get address based on latitude and longitudeXamarin android根据经纬度获取地址
【发布时间】:2016-07-21 05:05:55
【问题描述】:

我是 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 找到源代码

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

问候, 帕万

【问题讨论】:

    标签: android xamarin geocode


    【解决方案1】:
    • 始终检查 Geocoder 是否可用,因为这需要后台服务可用,并且它可能不可用,因为它未包含在基本 Android 框架中:

      Geocoder.IsPresent

    • 在 Google 的 Dev API 控制台中注册您的应用

    • 将您的 Google Map API 密钥添加到您的应用清单

    • 如果您正在使用 Google 的融合位置提供商(通过 Google Play 服务)并且需要地理编码,请确保您的应用具有 ACCESS_FINE_LOCATION 权限:

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

    • 根据您设备的Geocoder 服务,可能需要一个请求,“Google”或您设备的地理编码器服务才会回复地址列表。

      • 不要向服务发送垃圾邮件,您会受到限制,请使用越来越长的请求之间的时间延迟。

    注意:一个非常频繁的响应是:

    `Timed out waiting for response from server`
    

    在这种情况下,请稍等片刻,然后重试。

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

    注意:我通常使用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);
    

    【讨论】:

      【解决方案2】:

      这是您在 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();
                  }
      
      
      
              }
      
      
      
          }
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        • 1970-01-01
        • 2013-07-04
        • 2014-06-14
        • 1970-01-01
        相关资源
        最近更新 更多