0

I want to use coordinate of the actual location (CLLocationManager) to reverse geocoding (CLGeoCoder). I have this code:

        locationMgr = new CLLocationManager();
        locationMgr.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters;
        locationMgr.DistanceFilter = 10;
        locationMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) => {
            Task.latitude = e.NewLocation.Coordinate.Latitude;
            Task.longitude = e.NewLocation.Coordinate.Longitude;
            locationMgr.StopUpdatingLocation();
        };

        btnLocation = new UIBarButtonItem(UIImage.FromFile("Icons/no-gps.png"), UIBarButtonItemStyle.Plain, (s,e) => {
            if (CLLocationManager.LocationServicesEnabled) { 
                    locationMgr.StartUpdatingLocation();

                    geoCoder = new CLGeocoder();
                    geoCoder.ReverseGeocodeLocation(new CLLocation(Task.latitude, Task.longitude), (CLPlacemark[] place, NSError error) => {
                        adr = place[0].Name+"\n"+place[0].Locality+"\n"+place[0].Country;
                        Utils.ShowAlert(XmlParse.LocalText("Poloha"), Task.latitude.ToString()+"\n"+Task.longitude.ToString()+"\n\n"+adr);
                    });
            }
            else {
                Utils.ShowAlert(XmlParse.LocalText("PolohVypnut"));
            }
        });

Because UpdatedLocation() take some seconds, input of ReverseGeocodeLocation() is Task.latitude=0 and Task.longitude=0.

How can I wait for right values (Task.latitude, Task.longitude) before ReverseGoecodeLocation()?

Thanks for any help.

Fous
  • 1
  • 1
  • 2
  • wait until the UpdatedLocation event fires before starting your Reverse Geocode lookup. – Jason Dec 05 '12 at 12:58

1 Answers1

0

Your geocoder's ReverseGeocodeLocation method is called before the CLLocationManager gets a location.

Calling StartUpdatingLocation does not mean that the UpdatedLocation event will be triggered immediately. Furthermore, if you are on iOS 6, UpdatedLocation will never be triggered. Use the LocationsUpdated event instead.

Example:

locationManager.LocationsUpdated += (sender, args) => {

    // Last item in the array is the latest location
    CLLocation latestLocation = args.Locations[args.Locations.Length - 1];
    geoCoder = new CLGeocoder();
    geoCoder.ReverseGeocodeLocation(latestLocation, (pl, er) => {

        // Read placemarks here

    });

};
locationManager.StartUpdatingLocation();
Dimitris Tavlikos
  • 8,170
  • 1
  • 27
  • 31