获取当前位置和城市名称Xamarin.Android

时间:2016-04-08 09:12:13

标签: android xamarin geolocation location

我想使用Xamarin.Android获取当前位置和城市名称。我使用了此链接https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/中的代码,但我只获得了“纬度”和“经度”。也许我没有得到地址,因为我正在使用模拟器?

我想解决这个问题,并使用最少的代码获取当前的位置和地址(使用一些插件,api,也许..)

提前致谢:)

2 个答案:

答案 0 :(得分:0)

我想你可以看看 geocoder in android

 var addresses = await geo.GetFromLocationAsync (42.37419, -71.120639, 1);

此配方创建一个Geocoder实例,该实例位于Android.Locations命名空间中。 Geocoder使用纬度和经度调用GetFromLocationAsync以反转地理编码。这将异步执行网络调用,以便不阻止主UI线程。返回结果时,该方法将返回一个地址列表并继续在UI线程上。在此调用中返回1地址,因为这是传递给GetFromLocationAsync调用的第三个参数的数字。返回的地址包含有关位置的各种信息,包括街道地址。

答案 1 :(得分:0)

Xamarin有一篇很棒的技术文章,可以使用Geocoder在Android上完成整个过程。

参考:https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/

执行Long / Lat to Address查找的代码是:

async Task<Address> ReverseGeocodeCurrentLocation()
{
    Geocoder geocoder = new Geocoder(this);
    IList<Address> addressList =
        await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

    Address address = addressList.FirstOrDefault();
    return address;
}

仅供参考:大多数模拟器都有一个用户定义的模拟LatitudeLongitude的设置,因此您可以更改它并测试获得不同的响应。

即。在Xamarin的Android播放器中,有一个GPS设置对话框:

enter image description here

GenyMotion的Android Emulator等设置类似......

相关问题