2011-08-08 2 views
1

CLLocationManager을 처음 사용합니다. 나는 옳은 일을하고 있는지 잘 모르겠습니다. 내가 틀렸다면 나를 바로 잡아라.CLLocationManager startUpdating UIButton을 사용하여 위치

viewDidLoad 메서드에서 locationManager를 초기화하고 동일한 방법으로 locationManager를 startUpdating하도록 locationManager에 지시합니다.

대리자

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 

    [locationManager stopUpdatingLocation]; 
    //do stuff with the coordinates 
} 

이 델리게이트 메소드 호출을 반복을 피하기 위해 수신한다.

는 내가 가진 사용자가 위치

//called by user action 
-(IBAction)updateLocation{ 
    //start updating delegate 
    locationManager.delegate=self; 
    [locationManager startUpdatingLocation];  
} 

그러나 위치 변경을하고 난 UIbutton를 클릭하면 위치가이 donot 전혀 변경 좌표를 업데이트하기 위해 클릭 할 수있는 UIButton. :(내가 뭘 잘못

은?이 일의 오른쪽 아니면 내가 전혀?

도움말을 감상 할 수있다.

+0

을 delegate- : 애플의 샘플 코드는 대리자를 여러 번 호출하고 반환 된 위치의 정확성에 대해 원하는 정확성을 확인하여이 작업을 수행하는 방법을 가장 잘 보여줍니다 : HTTP : //developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html – iwasrobbed

답변

10

CLLocationManager이 마지막 위치를 캐시을 locationManager을 중단해서는 안되며로서 돌려줍니다 곧 -startUpdatingLocation으로 전화를 걸면 업데이트를 시작하고 이전 위치를 수신 한 다음 업데이트를 중지합니다.

위의 질문과 마찬가지로 위임자를 호출 할 때 잘못된 점이 있습니다. 방법을 여러 번 사용 하시겠습니까? 사용자가 버튼을 탭하면 CLLocationManager을 그대로두고 사용자가 버튼을 누를 때 CLLocationMangerlocation 속성을 확인하십시오. locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters : 당신이 등 전력 소모에 대한 걱정 때문에 당신이 대리자 메서드에 여러 통화를 피하기 위해 노력하고있는 이유 인 경우

는 같은과 CLLocationManagerdesiredAccuracy 속성을 조정합니다.

모든는 ... 같이 보일 수도 말했다

.H 파일 :

하는 .m 파일
@interface YourController : NSObject <CLLocationManagerDelegate> 

@property (nonatomic, retain) CLLocationManager *locationMgr; 
@property (nonatomic, retain) CLLocation *lastLocation; 

- (IBAction)getNewLocation:(id)sender; 

@end 

:

@interface YourController 

@synthesize locationMgr = _locationMgr; 
@synthesize lastLocation = _lastLocation; 

- (id)init 
{ 
    if (self = [super init]) { 
     self.locationMgr = [[CLLocationManager alloc] init]; 
     self.locationMgr.desiredAccuracy = kCLLocationAccuracyBest; 
     self.locationMgr.delegate = self; 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    if (!self.lastLocation) { 
     self.lastLocation = newLocation;   
    } 

    if (newLocation.coordinate.latitude != self.lastLocation.coordinate.latitude && 
     newLocation.coordinate.longitude != self.lastLocation.coordinate.longitude) {   
     self.lastLocation = newLocation; 
     NSLog(@"New location: %f, %f", 
       self.lastLocation.coordinate.latitude, 
       self.lastLocation.coordinate.longitude); 
     [self.locationMgr stopUpdatingLocation]; 
    } 
} 

- (IBAction)getNewLocation:(id)sender 
{ 
    [self.locationMgr startUpdatingLocation]; 
    NSLog(@"Old location: %f, %f", 
      self.lastLocation.coordinate.latitude, 
      self.lastLocation.coordinate.longitude); 
} 

- (void)dealloc 
{ 
    [self.locationMgr release]; 
    self.locationMgr = nil; 
    [self.lastLocation release]; 
    self.lastLocation = nil; 
    [super dealloc]; 
} 

@end 
+0

감사합니다. 그래서 stopUpdatingLocation을 제거하면 응용 프로그램이 자동으로 위치를 업데이트하고 사용자가 button.Am 맞을 때도 자동으로 업데이트됩니다. – Neelesh

+0

아니요, 응용 프로그램이 자동으로 * 위치를 업데이트 * 시작합니다. 이 답변을 더 자세히 보여주기 위해 편집하겠습니다 ... – GarlicFries

+0

하지만 사용자가 위치 업데이트를 시작하기를 원합니다 .. – Neelesh

0

암은 당신이 #import <CoreLocation/CoreLocation.h> 프레임 워크에 포함되어 있다고 가정 함께있어. 이것은 당신이 위치 업데이트를 받기 시작하는 방법입니다.

CLLocationManager *locationMgr = [[CLLocationManager alloc] init]; 
locationMgr.desiredAccuracy = kCLLocationAccuracyBest; 
locationMgr.delegate = self; 

[locationMgr startUpdatingLocation]; 

여기에 정확합니다. 이 후에는 위치 업데이트를 받기 시작, 여기에 나중에 참조

-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation 
{ 
    // Handle location updates 
} 

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    // Handle error 
} 
관련 문제