skip to Main Content

I’m trying to implement longitude and latitude in my sample app. I use iPhone 5s device to test it. It doesn’t show any effect on latitude and longitude labels on button actions.

Before compiling Xcode shows warning at line didUpdateToLocation: fromLocation: method as

"implementing deprecated method"

please help me with the suitable method which replaces this method for smooth working of the app

With Xcode 12.4 and for iOS 12.3 using objective c I’m trying to implement a simple location demo app which shows longitude and latitude. Below are my viewController.h and viewController.m files. And I’ve tried this below code from online tutorial AppCoda

viewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController :UIViewController<CLLocationManagerDelegate>
{
  IBOutlet UILabel *lattitudeLabel;
  IBOutlet UILabel *longitudeLabel;
  IBOutlet UILabel *addressLabel;
}
- (IBAction)getCurrentLocation:(UIButton *)sender;
@end

viewController.m

#import "ViewController.h"

@interface ViewController ()
{
  CLLocationManager *locationManager;
  CLGeocoder *geocoder;
  CLPlacemark *placemark;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
  locationManager = [[CLLocationManager alloc]init];
  geocoder = [[CLGeocoder alloc] init];
}


- (IBAction)getCurrentLocation:(UIButton *)sender
{
  locationManager.delegate = self;
  locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  [locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
  NSLog(@"Error: Failed to get location");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
lattitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
  
  [locationManager stopUpdatingLocation];
  
  NSLog(@"Resolving the Address");
  [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
      NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
      if (error == nil && [placemarks count] > 0) {
        self->placemark = [placemarks lastObject];
        self->addressLabel.text = [NSString stringWithFormat:@"%@ %@n%@ %@n%@n%@",
                       self->placemark.subThoroughfare, self->placemark.thoroughfare,
                       self->placemark.postalCode, self->placemark.locality,
                       self->placemark.administrativeArea,
                       self->placemark.country];
      } else {
      NSLog(@"%@", error.debugDescription);
      }
  } ];
}
@end

enter image description here

On clicking button get my location nothing is happening. It should actually update the co-ordinates besides Longitude: and Latitude: some float values..but it’s not doing that..I’ll ask please suggest me links to any tutorial web site or any GitHub project for location handling in iOS 12.3 and above in objective c…Thanks for the replies and suggested edits.

2

Answers


  1. As the docs say, just use locationManager(_:didUpdateLocations:)
    .

    Swift 5

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // Stop updates if this is a one-time request
        manager.stopUpdatingLocation()
        
        // Pop the last location off if you just need current update
        guard let newLocation = locations.last else { return }
        
        <... Do something with the location ...>
    }
    

    Objective-C

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
        [manager stopUpdatingLocation];
    
        CLLocation *newLocation = [locations lastObject];
        if (newLocation) {
            <... Do something with the location ...>
        }
    }
    
    Login or Signup to reply.
  2. I hope this implementation for delegate method will help you.
    works both on Mac and IOS. I used it to pinpoint current position for weather application.

    - (void)locationManager:(CLLocationManager *)manager
         didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
        if (locations.count > 0) {
            // Location manager has nonempty array of readings
            // We take the las one - the latest position determined
            CLLocation *last = locations[locations.count - 1];
            // Evaluate threshold distance to notice change
            CLLocationDistance distance = LOCATOR_SENSITIVITY + 1;
            if (previous) {
                 // calculate distance from previous location
                 distance = [last distanceFromLocation:previous];
            }
            // update previous location
            previous = last;
            if (distance > LOCATOR_SENSITIVITY) {
                // If we moved far enough, update current position
                currentLat = last.coordinate.latitude;
                currentLon = last.coordinate.longitude;
                // and notify all observers 
                // You can use delegate here, dispatch_async wrapper etc.
                [[NSNotificationCenter defaultCenter] 
                     postNotificationName:LocationIsChanged object:nil];
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search