2014-09-13 11 views
0

사용자 위도와 경도를 가져 와서 콘솔 로그에 올바르게 표시 할 수 있지만 도시 이름이나 기타로 역 지오 코딩을 되돌릴 수 없습니다. 그들과 함께.lat/long-throws 유형 오류를 사용하여 geolocate를 역전시킬 수 없습니다.

내가 뭘 잘못하고 있니?

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 

    CLLocation *userlatitude = [locations lastObject]; 
    CLLocation *userlongitude = [locations objectAtIndex:locations.count-2];   
    CLGeocoder *ceo = [[CLGeocoder alloc]init]; 
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:userlatitude longitude:userlongitude]; 

    [ceo reverseGeocodeLocation: loc completionHandler: ^(NSArray *placemarks, NSError *error) { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 
     NSLog(@"Currently located at %@",locatedAt); 
    }]; 

    [locationManager stopUpdatingLocation]; 
} 

는 또한

CLLocationDegrees userlatitude = [[locations lastObject] doubleValue]; 
CLLocationDegrees userlongitude = [[locations objectAtIndex:locations.count-2] doubleValue]; 

CLLocation *userlatitude .... objectAtIndex:location.count-2];를 교체 시도했지만 (이 코드 편집기에서 구문 오류를 표시하지 않는 경우에도)이 런타임시 오류를 반환합니다.

답변

1

didUpdateLocations 메서드는 위치 데이터가 포함 된 CLLocation 개의 개체 배열을 반환합니다. 이 배열에는 항상 현재 위치를 나타내는 객체가 하나 이상 포함됩니다.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    CLGeocoder *ceo = [[CLGeocoder alloc]init]; 

    [ceo reverseGeocodeLocation:(CLLocation *)[locations lastObject] // pass the most recent value 
       completionHandler: ^(NSArray *placemarks, NSError *error) { 

        if (error) { 
         NSLog(@"reverse error: %@", error); 
         // handle error here 
         return; 
        } 

        CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
        NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] 
             componentsJoinedByString:@", "]; 

        NSLog(@"Currently located at %@",locatedAt); 

    }]; 

}