Forum Xamarin Xamarin.Forms

Problem with Geolocator and location in Android

andrzejthingstadandrzejthingstad USMember
edited February 2015 in Xamarin.Forms

I try to geocode, reverse geocode and get location from devices in my project. I use Xamarin.FormsMaps for geocoding with this code:

Geocoder geo = new Geocoder (); var pos = new Xamarin.Forms.Maps.Position (63.430961, 10.407761); var address = await geo.GetAddressesForPositionAsync (pos); foreach (var a in address) { Console.WriteLine ("Adres: " + a.ToString());

To find the location of my device I use this in the MainActivity:

var resolverContainer = new SimpleContainer (); resolverContainer.Register<IGeolocator, Geolocator>(); Resolver.SetResolver (resolverContainer.GetResolver ());

And when I want to use the Geolocator I call it like this in my shared project:

var geo = Resolver.Resolve<IGeolocator> ();

To get the location of my device I have this method:

`           var token = new System.Threading.CancellationToken ();
        var geo = Resolver.Resolve<IGeolocator> ();
        geo.StartListening (3000, 0, true);

        geo.PositionError += OnListeningError;
        geo.PositionChanged += OnPositionChanged;

        string lat;
        string lng;

        await geo.GetPositionAsync (10000, token, true)
            .ContinueWith (t => {
                if (t.IsFaulted)
                    Console.WriteLine (((GeolocationException)t.Exception.InnerException).Error.ToString ());
                else if (t.IsCanceled)
                    Console.WriteLine ("Canceled");
                else {
                    lat = "La: " + t.Result.Latitude.ToString ();
                    lng = "Lo: " + t.Result.Longitude.ToString ();
                    Console.WriteLine("Position is: " + lat + ", " + lng);
            }, scheduler);`

This code works fine on my iOS device with iOS 8 installed and on a Samsung Galaxy Active S4 with Android 4.4.2 installed. However with a Samsung Galaxy S2 with 4.1.2 installed this wont work. I dont get any error messages but it just crashes when I try to use the methods. I have targeted Android version on 21 and set the Java heap size to 1G. I have also added permissions. Do anyone have any ideas or tips to how I can maybe do this in a different way?


  • PaulDistonPaulDiston USUniversity ✭✭✭✭


    Have you stepped through the code? Do you know which line the application crashes at?


    Paul Diston

  • @PaulDiston on the geocode and the reverse geocoding the application crashes at var address = await geo.GetAddressesForPositionAsync (pos); and var location = await geo.GetPositionsForAddressAsync (address);.

    When I try to get the location of the device, the application crashes at await geo.GetPositionAsync (10000, token, true). So everytime I try to use the Gecoder or the Geolocator.

Sign In or Register to comment.