0

이 여기에 전체 방법은, 난 원래 하반기를 두지 않았다 반환하는 것은 작동합니다코어 위치 나는 그것을 알고 있기 때문에 00

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    CLLocation *location = [locationManager location]; 

    CLLocationCoordinate2D coordinate = [location coordinate]; 
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude]; 
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude]; 
    double myLatitude2 = [myLatitude doubleValue]; 
    double myLongitude2 = [myLongitude doubleValue]; 


    NSArray *array = [dataHold objectForKey:@"Subtree"]; 
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]]; 
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]]; 
    double clubLatitude = [latitude doubleValue]; 
    double clubLongitude = [longitude doubleValue]; 
    CLLocationCoordinate2D start = { myLatitude2, myLongitude2};  
    CLLocationCoordinate2D destination = { clubLatitude, clubLongitude};   
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 

} 
+3

좋아, 왜 세상에서 당신이 언 박싱/복싱입니까? 'NSNumber'를 어디에서 사용할 지에 대한 재교육이 필요할 수도 있습니다 : -/ –

답변

1

두 가지 문제 :

1) 당신은 NSNumber 상자를하고있다/unbox cycle 아무 것도 사용하지 않음

2) 코어 위치가 비동기이므로 사용자의 위치를 ​​즉시 반환 할 수 없으며 사용 가능한 경우 위임 방법을 사용하여 위치를 검색해야합니다.

은 다음의 예 어떻게 해야 일 :

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    [locationManager startUpdatingLocation]; 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    CLLocationCoordinate2D coord = [newLocation coordinate]; 
    NSArray *array = [dataHold objectForKey:@"Subtree"]; 
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]]; 
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]]; 
    double clubLatitude = [latitude doubleValue]; 
    double clubLongitude = [longitude doubleValue]; 
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 
} 
+0

나는 그것을 새롭게 했습니까? – Eric

+0

위치 사과 샘플 코드가 왜 그렇게 간단합니까? – Eric

+0

CLLocationCoordinate2D 좌표 = [위치 좌표]; // NSLog (@ "% @", 좌표); \t [이벤트 setLatitude : [NSNumber numberWithDouble : coordinate.latitude]]; \t [이벤트 setLongitude : [NSNumber numberWithDouble : coordinate.longitude]]; – Eric

1

문제가 시작되는 코드의 네 번째 줄입니다. 위치를 즉시 알 수 없으므로 위치 서비스가 위치를 사용할 수 있음을 델리게이트에 알릴 때까지 기다려야합니다. 당신이 "자기"로 대리자를 설정하기 때문에,

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    // this is not where you should be looking 
    CLLocation *location = [locationManager location]; 

    // the rest of this function is not included in this example ... 
} 

현재 클래스를하지만, 다음과 같은 두 가지 기능을 정의하는 "CLLocationManagerDelegate"프로토콜 구현 : 내가 설명하는 코드에 약간의 주석을 추가 할 것입니다

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { /* */ } 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { /* */ } 

여기서 제공되는 위치 정보를 얻을 수 있습니다.

@interface MyClass : NSObject <CLLocationManagerDelegate> { 

을 그리고 당신은 완벽하게 대표하고 당신이 새로운 경우 괜찮 그들이 제공하는 목적을 이해하지 못하는 것처럼 보조 노트, 그것은 보인다

망가과 같이 클래스 정의를 업데이트하는 것을 잊지 Objective-C에 익숙해 져 있지만 익숙해지면 인생이 좀 더 쉬워 질 것입니다.

일부 기타 유용한 물건 :

+0

도움을 주시면 감사하겠습니다.로드되는보기는지도 응용 프로그램으로 다시 전송하는보기입니다. 나는 무한 루프에 걸렸다. – Eric

+0

디자인 결정이며 코어 위치와 관련이 없습니다. UIApplicationDelegate (응용 프로그램 대리자에서 일부 기능을 구현했을 것입니다)에서 "application : didFinishLaunchingWithOptions :"를 사용하여 응용 프로그램이 URL로 시작되었는지 확인한 다음 다시 시작하지 않는지 확인하십시오. – Nippysaurus

+0

http : //developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html # // apple_ref/occ/intfm/UIApplicationDelegate/application : didFinishLaunchingWithOptions : – Nippysaurus

관련 문제