如何将用户输入的地址转换为 LatLng Xamarin.Android

How to convert Address from user input into LatLng Xamarin.Android

我正在尝试在单击“搜索”按钮时将用户在“编辑文本”中输入的内容转换为 LatLng。然后这将更新相机并移动帽子位置。 这是我的主要代码:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;
using Android.Views;
using Android.Gms.Maps.Model;
using System.Collections.Generic;
using Android.Locations;
using System.Linq;


namespace SafeandSound
{
[Activity(Label = "SafeandSound", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    private GoogleMap mMap;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);


        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        SetUpMap();
    }
    private void SetUpMap()
    {
        if (mMap == null)
        {
            FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);
        }
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        mMap = googleMap;

    }
    // Button to Search for Address//
    public void onMapSearch(View view)
    {
        EditText address = (EditText)FindViewById(Resource.Id.searchText);
         var addressnew = address.Text;
        if (addressnew != null)
        {
            addressnew = address.Text;
        }
        Geocoder geoCoder = new Geocoder(this);
        IList<Address> coordinates = geoCoder.GetFromLocationName(addressnew, 0);
        Address gotAddress = coordinates.FirstOrDefault();
        LatLng latLng = new LatLng(gotAddress.Latitude, gotAddress.Longitude);
        CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
        builder.Target(latLng);
        builder.Zoom(10);
        CameraPosition cameraPosition = builder.Build();
        CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

    }
}
}

当我现在使用它时,出现异常错误。请帮忙!!!

首先您需要通过静态方法检查 Geocoder 服务是否在 device/emulator 上可用:

Geocoder.isPresent

注意:使用 Geocoder 需要 Internet 访问和 Google 要安装的 Play 服务...

接下来您在 "maxResults" 参数中请求零结果:

GetFromLocationName(addressnew, 0);

int: max number of results to return. Smaller numbers (1 to 5) are recommended

此外,您可能需要重试请求才能获得结果。你不应该敲打服务,因为你会受到限制。使用每次尝试后增加的重试延迟。

示例:

if (!Geocoder.IsPresent)
{
    Log.Error("SO", "Geocoder is not present");
}
else
{
    var geocoder = new Geocoder(this);
    var retry = 0;
    do
    {
        var addressList = await geocoder.GetFromLocationNameAsync("Starbucks 523 Pine Street, Seattle, WA, 98101", 5);
        if (addressList.Count > 0)
        {
            foreach (var address in addressList)
            {
                Log.Debug("SO", $"{address.Latitude}:{address.Longitude} - {address.FeatureName} : {address.GetAddressLine(0)} : {address.GetAddressLine(1)}");
            }
            break;
        }
        retry++;
        Log.Warn("SO", $"No addresses returned...., retrying in {retry * 2} secs");
        await Task.Delay(retry * 1000);
    } while (retry < 5);
}

输出:

[SO] 47.611423:-122.337519 - Starbucks : Starbucks : 400 Pine Street
[SO] 47.611848:-122.335693 - Starbucks : Starbucks : 515 Pine Street