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
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
As the docs say, just use locationManager(_:didUpdateLocations:)
.
Swift 5
Objective-C
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.