如何从Lat Long获取地址

loading...


0

您好我正在为iOS和Android创建应用程序 . 我希望在页面上的加载中修复该问题,我想显示带有地址的 Map . 我可以成功获得Lat Long但我无法获得我想在标签上显示的地址 . 以下是我正在使用的代码 .

using Plugin.Geolocator;
using Xamarin.Forms.Maps;

  private Position _position;
    // Map map;
    double Lat, Long;
    string address = "";

   public TestPage()
    {
        NavigationPage.SetHasNavigationBar(this, false);
        InitializeComponent();

        GetPosition();
        if (_position != null)
        {
            Lat = _position.Latitude;
            Long = _position.Longitude;
        }
        //Task<String> str = GetAddress();
        map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(Lat, Long), Distance.FromMiles(1)));
        var position = new Position(Lat, Long);
        var pin = new Pin
        {
            Type = PinType.Place,
            Position = position,
            Label = ""                
        };
        map.Pins.Add(pin);
        var zoomLevel = 17.83;
        var latlongdegrees = 360 / (Math.Pow(2, zoomLevel));

        map.MoveToRegion(new MapSpan(position, latlongdegrees, latlongdegrees));

        LabelTime.Text = DateTime.Now.ToString("hh:mm tt") + " " + DateTime.Now.ToString("MM/dd/yyyy");
        string recentAddress = address;  // trying to get adderss for location 
    }

GetPosition()

public async void GetPosition()
    {
        Plugin.Geolocator.Abstractions.Position position = null;
        try
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100.0;

            position = await locator.GetLastKnownLocationAsync();

            if (position != null)
            {
                _position = new Position(position.Latitude, position.Longitude);
                //got a cahched position, so let's use it.
                return;
            }

            if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
            {
                //not available or enabled
                return;
            }

            position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10), null, true);

        }
        catch (Exception ex)
        {
            // error(ex.Message, Convert.ToString(ex.InnerException), ex.Source, ex.StackTrace);
        }
        _position = new Position(position.Latitude, position.Longitude);
        if (position == null)
            return;

    }

GetAddress()

public async Task<String> GetAddress()
        {
           // string Addrsss = "";
            try
            {
                Geocoder geoCoder = new Geocoder();

                if (_position != null)
                {
                    var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(_position);
                    await Task.Delay(2000);
                    foreach (var a in possibleAddresses)
                    {
                        address += a + "\n";
                    }
                }
                return address;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

我也尝试在 OnAppearing() 上获取当前地址

protected override void OnAppearing()
    {
        this.Appearing += TestPage_Appearing; //Subscribe to event
        base.OnAppearing();
    }

    protected async void TestPage_Appearing(object sender, EventArgs args)
    {
         GetPosition();
        address= await GetAddress();
        string a = address;
        this.Appearing -= TestPage_Appearing; //Unsubscribe 
    }

loading...

1回答

  • 0

    您可以使用Xamarin.Essentials获取地标地址,如下所示,

    private void GetAddress()
        {
            var lat = 47.673988;
            var lon = -122.121513;
    
            var placemarks = await Geocoding.GetPlacemarksAsync(lat, lon);
    
            var placemark = placemarks?.FirstOrDefault();
            if (placemark != null)
            {
                var geocodeAddress =
                    $"AdminArea:       {placemark.AdminArea}\n" +
                    $"CountryCode:     {placemark.CountryCode}\n" +
                    $"CountryName:     {placemark.CountryName}\n" +
                    $"FeatureName:     {placemark.FeatureName}\n" +
                    $"Locality:        {placemark.Locality}\n" +
                    $"PostalCode:      {placemark.PostalCode}\n" +
                    $"SubAdminArea:    {placemark.SubAdminArea}\n" +
                    $"SubLocality:     {placemark.SubLocality}\n" +
                    $"SubThoroughfare: {placemark.SubThoroughfare}\n" +
                    $"Thoroughfare:    {placemark.Thoroughfare}\n";
    
                Console.WriteLine(geocodeAddress);
            }
        }
    

    请参阅link以获取详细信息 . 另请参阅this one以获取详细实施信息

评论

暂时没有评论!