2012-11-12 3 views
2

MKReverseGeocoder가 더 이상 사용되지 않습니다. 질문은 CLGeocoder를 어떻게 가장 쉽게 변경할 수 있습니까? 극도로 긴 소스 코드 (나는 아주 간단하다고 생각한다. 그러나 이것에 대한 새로운 것)에 대해 유감스럽게 생각한다. 이 ... 내가 전에 가지고 무엇을 천 번 MKReverseGeocoder가 더 이상 지원되지 않습니다.

감사 StoreLocation.h

@interface StoreLocation : NSObject <MKAnnotation> { 

    CLLocationCoordinate2D coordinate; 

} 

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readwrite) NSString *subtitle; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate; 

- (NSString *)subtitle; 

- (NSString *)title; 

@end 

StoreLocation.m

@implementation StoreLocation 
@synthesize coordinate,subtitle; 

-(NSString *)subtitle{ 

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; 
    if ([userDef boolForKey:@"SavedAddress"]) { 
     NSString *savedAddress = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavedAddress"]; 
     return savedAddress; 
    } 
    else { 
     return subtitle; 
    } 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D) coor{ 
    self.coordinate=coor; 
    return self; 
} 


- (void)setCoordinate:(CLLocationCoordinate2D)coor { 
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coor]; 
    geocoder.delegate = self; 
    coordinate = coor; 
    [geocoder start]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { 
    NSLog(@"fail %@", error); 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { 
    self.subtitle = [placemark.addressDictionary valueForKey:@"Street"]; 

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; 
    [userDef setValue:subtitle forKey:@"SavedAddress"]; 
} 

MapViewController.m

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

    MKGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:location]; 
     [geocoder start]; 
    } 


    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{ 
     NSString *streetAddress = [NSString stringWithFormat:@"%@, %@", 
            [placemark.addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey], 
            [placemark.addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey]]; 

     mapView.userLocation.subtitle = streetAddress;  
    } 
-(IBAction)storeLocation { 

    StoreLocation *position=[[StoreLocation alloc] initWithCoordinate:location]; [mapView addAnnotation:position]; } 


    - (MKAnnotationView *)mapView:(MKMapView *)mapview 
       viewForAnnotation:(id <MKAnnotation>)dropPin 
    { 
     if ([dropPin isKindOfClass:MKUserLocation.class]) 
     { 
      return nil; 
     } 

     MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapview dequeueReusableAnnotationViewWithIdentifier:@"annot"]; 
     if (!pinView) 
     { 
      pinView = [[MKPinAnnotationView alloc] initWithAnnotation:dropPin reuseIdentifier:@"annot"]; 
      pinView.canShowCallout = YES; 
     } 
     else { 
      pinView.annotation = dropPin; 
     } 
     return pinView; 
    } 

입니다!

답변

2

어쩌면 이렇게 할 수 있을까요?

iOS4 이후 모든 펌웨어에서 MKReverseGeocoder가 사용되지 않습니다. 이것은 단지 구식 수업을 사용하는 것이 이제는 쓸모 없게되고 눈살을 찌푸리게된다는 것을 의미합니다. 대신 CLGeocoder를 사용하십시오 :

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

    [geocoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead 
        completionHandler:^(NSArray *placemarks, NSError *error) { 

         dispatch_async(dispatch_get_main_queue(),^ { 
          // do stuff with placemarks on the main thread 

         if (placemarks.count == 1) { 

         CLPlacemark *place = [placemarks objectAtIndex:0]; 


         NSString *zipString = [place.addressDictionary valueForKey:@"ZIP"]; 

         [self performSelectorInBackground:@selector(showWeatherFor:) withObject:zipString]; 

         } 

}); 

}]]; 지오 코딩을 되돌리고 싶은 경우 좌표의 하드 쌍 perse -

당신의 위도와 경도를 가진 CLLocation 위치를 초기화 : 나는 또한 당신은 여전히 ​​MKReverseGeocoder을 사용할 수 있다는 점을 지적 할

CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; 

. 그래도 향후 iOS 업데이트로 제거 될 수 있습니다.

+0

알았습니다! 감사. – Christoffer

+1

붙여 넣기 복사 : http://stackoverflow.com/questions/11832150/mkreversegeocoder-deprecated-in-ios-5 – Rushi

관련 문제