Objective-C源locationManager在自己的类中 [英] objective-c source locationManager out in own class

查看:63
本文介绍了Objective-C源locationManager在自己的类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个自己的类中派发locationManager,可以从其他不同的类调用它.

I want to source the locationManager out in an own class that I can call from different other classes.

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

What I did. Created an iPhone Application, and added the locationManger code in the ViewController. Worked.

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

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.

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

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.

#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

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

推荐答案

问题在于meinelokation在getLocation运行之后就被释放了,因为它只是一个局部变量.创建一个性能很强的meineLokation,然后它应该可以正常工作.

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.

这篇关于Objective-C源locationManager在自己的类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆