objective-c源位置管理器在自己的类中



我想在自己的类中找到locationManager,我可以从不同的其他类中调用它。

我做了什么。创建了一个iPhone应用程序,并在ViewController中添加了locationManger代码。工作。

现在,我创建了一个类Lokation,将所有locationManager代码转移到这个新类中,并从ViewController调用该代码。结果:函数getLocation被调用,但locationmanager:manager didUpdateToLocation:newLocation fromLocation:oldLocation未被调用。

输出

2013-07-03 15:44:114.124 Sandbox2[41374:c07]内部Lokation::getLocation

缺少什么?我如何将其集成?有些东西告诉我"代表",但我在这个话题上是新手。

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate> 
@end 

ViewController.m(20130704更新为工作代码)

#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

Lokatation.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

问题是meinelokation在getLocation运行后立即被释放,因为它只是一个局部变量。创建一个强大的属性meineLokation,然后它应该可以正常工作。

最新更新