0

I am trying to get my current location and the reverseGeoCode it for an address but I keep getting my location as long 00000.00 and lat 00000.00 Here is my code

locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    currentLocation = [[CLLocation alloc]initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude]; //insert your coordinates
    NSLog(@"loc:%@",currentLocation);
    [ceo reverseGeocodeLocation:currentLocation
              completionHandler:^(NSArray *placemarks, NSError *error) {
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  if (placemark) {
                     
                      NSString *strString =placemark.name;
                      NSLog(@"strString:%@",strString);
                      NSString *midstring = [strString stringByAppendingString:@","];
                      if (placemark.postalCode == nil) {
                          self->postalcodeString = @"00000";
                      }
                      else{
                          self->postalcodeString = placemark.postalCode;
                      }
                      
                      self->combineString = [midstring stringByAppendingString:self->postalcodeString];
                      NSLog(@"combineString:%@",self.combineString);
                  }
user1114881
  • 731
  • 1
  • 12
  • 25
  • You need to handle the location in the delegate methods, not right after you call `startUpdatingLocation`, since the location manager might not yet have available information at this time. – Cristik Feb 11 '21 at 19:47
  • I moved the location manager to the viewDidAppear but I still got 0s. So I added `code` - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *location = [locations lastObject]; NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude); } `code` but when I compared it to the addresses in my contacts it just kept looping so I added `code`[locationManager stopUpdatingLocation]; `code` and it worked but kind of cumbersome. wish I could just get my current location once but its working – user1114881 Feb 11 '21 at 22:13

0 Answers0