1

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

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

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

输出

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

缺少什么以及如何整合它?有些东西告诉我“代表”,但我是这个话题的菜鸟。

视图控制器.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

定位.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

定位.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
4

1 回答 1

2

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

于 2013-07-03T14:31:06.570 回答