Xamarin Forms Geocode Android

2019-08-20 13:18发布

I Have two pieces of code, one to get adresses from a position & one to get position from adress.

GetAddressesForPosition

 var geoCoder = new Geocoder();

            Task.Run(async () =>
            {
                var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(position);

                Device.BeginInvokeOnMainThread(() =>
                {
                    var firstAdress = possibleAddresses.FirstOrDefault();
                    GeolocationAdressEntry.Text = firstAdress;
                    //GeolocationAdressEntry.Text = $@"{firstAdresse.} {firstAdresse.PostalCode} {firstAdresse.Locality}";
                });

            });

For this code, I not have any exception and the code never goes after

var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(position); 

GetPositionsForAddress

var elem = (Entry)sender;
            var geoCoder = new Geocoder();

            IEnumerable<Xamarin.Forms.Maps.Position> loc;

            try
            {
                loc = await geoCoder.GetPositionsForAddressAsync(elem.Text);

                var firstLoc = loc.FirstOrDefault();
                var adress = elem.Text;

                CreateNewPin(firstLoc, adress);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

For this one it thrown an error : "grpc failed". If needed I can post the complete error.

This is boring because it works perfectly in IOS.

I tried on emulators ( Genymotion & adb ) with different versions and also in a true device. Same issue.

1条回答
来,给爷笑一个
2楼-- · 2019-08-20 14:01

The issue comes from the emulator. Code work in a true device.

查看更多
登录 后发表回答