混合Geolocator和forms.maps xamarin.forms

时间:2016-04-19 15:22:22

标签: xamarin maps xamarin.forms

我需要一个简单的代码来提供GPS位置并将其转换为地址。

我找到了一个Geolocator库和Forms.Maps,我正在混合它们。一切似乎都没问题,除非我真的想从坐标中获取地址。标签永远不会收到地址。

这是代码

public partial class PublicarCarona : ContentPage
{
    Geocoder geoCoder;

    public PublicarCarona()
    {
        InitializeComponent();

        geoCoder = new Geocoder();
    }

    public async void OnReverseGeocodeButtonClicked(object sender, EventArgs e)
    {

        try
        {

            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;
            var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
            double? latitude = Convert.ToDouble (position.Latitude);
            double? longitude = Convert.ToDouble (position.Longitude);

            if (latitude != null && longitude != null)
            {
                var revposition = new Xamarin.Forms.Maps.Position(latitude.Value, longitude.Value);
                var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(revposition);
                foreach (var address in possibleAddresses)
                    lblcoordenadas.Text += address + "\n";
            }
            else lblcoordenadas.Text += "erro";

        }
        catch (Exception ex)
        {
            await DisplayAlert("Notification", "Unable to get GPS Location " + ex, "Ok");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果标签文字没有改变,这意味着geoCoder无法找到任何地址且possibleAddresses为空。

请注意,在Android中,您的应用需要ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限。如果您是目标api级别23(Android Marshmallow),则需要在运行时请求这些权限。您可以使用Permissions Plugin在运行时请求权限。

在iOS上,您需要将NSLocationWhenInUsageDescription键添加到Info.plist文件中。

请参阅此博文中的“平台设置”部分:Geolocation for iOS, Android, and Windows Made Easy

答案 1 :(得分:0)

这是androidmanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="AppCaronaSurf">
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBoxE8NxaTEl52vOo6l-MJiZN806XOaNsM" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />   
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>