I'm trying to create a helper class to get the coordinates of the phone in any other class easily. I've followed a tutorial in which the UIViewController implemented the <CLLocationManagerDelegate> and it worked. I tried to do the same in a simple NSObject, but then my delegate was not called anymore.
This is the code I have :
PSCoordinates.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface PSCoordinates : NSObject <CLLocationManagerDelegate>
#property (nonatomic, retain) CLLocationManager* locationManager;
#end
PSCoordinates.m
#import "PSCoordinates.h"
#implementation PSCoordinates
- (id) init {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(#"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Géolocalisation : %#",[newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(#"Géolocalisation (erreur) : %#",[error description]);
}
#end
I'm calling it by calling
PSCoordinates * coordinates = [[PSCoordinates alloc] init];
when pressing a button. The init is working as I can see the NSLog PSCoordinates init.
I've found other topics of people having the same problem but none of the answer solved it.
Your help would be really appreciated.
Make "PSCoordinates * coordinates" as global in your class. It will work :)
Related
I am trying to make a simple app the will get the users longitude and latitude
I followed the tutorial here:
http://www.appcoda.com/how-to-get-current-location-iphone-user/
and came with this:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
#property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
- (IBAction)getCurrentLocation:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
- (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];
_latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
#end
My issue is that "Would like to use your current location" popup.
and none of the delegate methods are being hit, at all. I put a break point at the beginning of each delegate method and nothing. Please help.
Please ensure you have following keys in your info.plist:
NSLocationAlwaysUsageDescription with value I need Location
NSLocationWhenInUseUsageDescription with value I need Location
privacy - location usage description with value I need Location
Ensure the info.plist details as answered earlier and then add the below code during your initialization;
if (IS_OS_8_OR_LATER) {
[locationmanager requestWhenInUseAuthorization];
//or (as per your app requirement)
[locationmanager requestAlwaysAuthorization];
}
I am trying to build a library for GPS in iOS using XCODE, But it is not working as a library, didUpdateToLocation is not called when I used in client application, but it is working fine as a application programm. I am not getting where I have done a mistake.
This is my application side code, where I have imported library code
- (void)viewDidLoad
{
[super viewDidLoad];
LocLibrary *mFunctions = [[LocLibrary alloc] init];//Creating a object
[mFunctions startGps];// Calling library method
}
Below is my LocLibrary.h
#interface LocLibrary : NSObject <CLLocationManagerDelegate>
{
CLLocationManager *locMgr;
int latitude,longitude;
}
-(void)startGps;
#property (strong, nonatomic) CLLocationManager *locMgr;
#property (strong, nonatomic) CLLocation *currentLocation;
#end
Below is LocLibarry.m code, didUpdateLocation method is not working
-(void)startGps
{
NSLog(#"getGps");
locMgr = [[CLLocationManager alloc] init];
NSLog(#"getGps1");
locMgr.delegate = self;
locMgr.distanceFilter=kCLDistanceFilterNone;
locMgr.desiredAccuracy = kCLLocationAccuracyBest;
[locMgr startUpdatingLocation];//Upto this working, it is not calling below code
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //This method not working
self.currentLocation = newLocation;
NSLog(#"Success");
if(newLocation.horizontalAccuracy <= 100.0f)
{
[locMgr stopUpdatingLocation];
NSLog(#"Stopped");
}
}
Check below line in viewDidLoad method you are creating object Of LocLibrary2 and assigning it to LocLibrary.
LocLibrary *mFunctions = [[LocLibrary2 alloc] init];//Creating a object
A ViewController starts a LocationTracking. I want to NSLog the location updates (when the simulator is on freeway drive), but didUpdateLocations only logs when I uncomment the NSTimer, I can't figure out why. I feel real dumb asking this, it's probably something simple, I'm new to this and have spent a long time trying to work it out.
ViewController:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"locationUpdate"]) {
LocationTracking *locationTracker = [[LocationTracking alloc] init];
[locationTracker startTracking];
NSLog(#"location tracking did");
//NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:locationTracker selector:#selector(logLast) userInfo:nil repeats:YES];
}
}
LocationTracking.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface LocationTracking : NSObject <CLLocationManagerDelegate>
#property (nonatomic, strong) NSMutableArray *locations;
-(void) startTracking;
-(void) logLast;
#end
LocationTracking.m:
#import "LocationTracking.h"
#interface LocationTracking()
#property (nonatomic, strong) CLLocationManager *locationManager;
#end
#implementation LocationTracking
-(void)startTracking
{
self.locations = [[NSMutableArray alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"did update %#",[locations lastObject]);
}
-(void)logLast
{
}
The following code works fine for me:
#import "ViewController.h"
#import CoreLocation;
#interface ViewController () <CLLocationManagerDelegate>
#property (strong,nonatomic) CLLocationManager *locationManager;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = [locations lastObject];
NSString *lat = [NSString stringWithFormat:#"%.2f",loc.coordinate.latitude];
NSString *lon = [NSString stringWithFormat:#"%.2f",loc.coordinate.longitude];
NSLog(#"location is: %#, %#",lat,lon);
}
#end
And if you are using the simulator in Xcode, don't forget to set the location for the simulator. It defaults to "None" for new projects.
I was trying to implement a separate class for managing my location. I wanted to get my location whenever I click a button.
gpsFilter.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface gpsFilter : NSObject <CLLocationManagerDelegate>
#property (nonatomic, retain) CLLocationManager *gpsManager;
#property (nonatomic, retain) NSString * latitude;
#property (nonatomic, retain) NSString * longitude;
#end
gpsFilter.m.
#import "gpsFilter.h"
#implementation gpsFilter
- (id) init{
self = [super init];
if(self != nil){
self.gpsManager = [[CLLocationManager alloc] init];
self.gpsManager.delegate = self;
[self.gpsManager startUpdatingLocation];
BOOL enable = [CLLocationManager locationServicesEnabled];
NSLog(#"%#", enable? #"Enabled" : #"Not Enabled");
}
return self;
}
- (void)gpsManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil){
self.latitude = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
self.longitude = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
- (void)gpsManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
#end
I am using a separate class because I wanted to add smoothing filter in the future. I don't get any updates. The NSLog is never triggered. I think some variable is getting autoreleased, but I am not sure, which one.
The viewController code is as follows.
#import "gpstestViewController.h"
#interface gpstestViewController (){
}
#end
#implementation gpstestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.location = [[gpsFilter alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)getloca:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];
}
- (IBAction)getLocation:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];
}
#end
I am sorry that I am dumping a lot of code, but I am a newbie to ios programming and I am not able to find what the problem is.
EDIT: The updateLocation delegate method is not being called at all.
The delegate methods must be named locationManager:didUpdateToLocation:fromLocation: and locationManager:didFailWithError:.
You cannot use custom method names like gpsManager:didUpdateToLocation:fromLocation:.
Also note that locationManager:didUpdateToLocation:fromLocation: is deprecated as of iOS 6 and you should use locationManager:didUpdateLocations:.
The signature of your didUpdateToLocation is wrong:
This is my code:
/** Delegate method from the CLLocationManagerDelegate protocol. */
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// here do
}
Further set the desiredAccuracy to CLLocationAccuracyBest.
I essentially started off with this CoreLocation iOS tutorial to get my head around how to implement CoreLocation into my app. (http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Location_Application)
But what I have encountered when trying to incorporate this tutorial into my app is that it now just keeps on looping over and over, which is just baffling me. Can anyone please help?
GPSViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface GPSViewController : UIViewController
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLLocation *startLocation;
#end
GPSViewController.m
#import "GPSViewController.h"
#import "DataClass.h"
#interface GPSViewController ()
#end
#implementation GPSViewController
#synthesize locationManager, startLocation;
DataClass *obj;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//initialization og global varable.
DataClass *obj=[DataClass getInstance];
//GPS Initialise
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
startLocation = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.latitude];
//latitude.text = currentLatitude;
obj.Latatude = currentLatitude;
NSString *currentLongitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.longitude];
//longitude.text = currentLongitude;
obj.Longitude = currentLongitude;
NSLog(#"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
if(obj.Latatude != NULL && obj.Longitude != NULL){
[self performSegueWithIdentifier:#"GPSSuccess" sender:self];
}
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.startLocation = nil;
self.locationManager = nil;
}
#end
You should call [locationManager stopUpdatingLocation]; in order to stop it from getting the user's location over and over.