问题
I want to source the locationManager out in an own class that I can call from different other classes.
What I did. Created an iPhone Application, and added the locationManger code in the ViewController. Worked.
Now I created a class Lokation
where I shifted all locationManager Code to this new class and called that code from the ViewController. Result: The function getLocation
is called but locationmanager:manager didUpdateToLocation:newLocation fromLocation:oldLocation
is not called.
Output
2013-07-03 15:44:14.124 Sandbox2[41374:c07] Inside Lokation::getLocation
What is missing and how do I integrate it? Something tells me "delegate", but I am a rooky in this topic.
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@end
ViewController.m (20130704 updated to working code)
#import "ViewController.h"
#import "Lokation.h"
@interface ViewController ()
// 20130704 added property
@property (strong, nonatomic) Lokation *lokation;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 20130704 corrected call to getLocation
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
@end
Lokation.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface Lokation : NSObject <CLLocationManagerDelegate> {
CLLocationDegrees currentLat;
CLLocationDegrees currentLng;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *currentLoc;
-(void)getLocation;
@end
Lokation.m
#import "Lokation.h"
@implementation Lokation
@synthesize locationManager = _locationManager;
@synthesize currentLoc = _currentLoc;
-(void)getLocation {
NSLog(@"Inside Lokation::getLocation");
// active location determination
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// We don't want to be notified of small changes in location,
// preferring to use our last cached results, if any.
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Inside Lokation::locationManager");
if (!oldLocation ||
(oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {
currentLat = newLocation.coordinate.latitude;
currentLng = newLocation.coordinate.longitude;
} else { // oldLocation
currentLat = oldLocation.coordinate.latitude;
currentLng = oldLocation.coordinate.longitude;
}
self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];
NSLog(@"currentLat: %f currentLng %f", currentLat, currentLng);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
@end
回答1:
The problem is that meinelokation is being deallocated right after getLocation runs, because it's just a local variable. Create a strong property meineLokation, and then it should work properly.
来源:https://stackoverflow.com/questions/17449923/objective-c-source-locationmanager-out-in-own-class