2012-11-12 219 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

也許這樣?

MKReverseGeocoder在iOS4之後的所有固件中都被棄用。這僅僅意味着它現在已經過時,並且不敢使用過時的課程。改爲使用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]; 

         } 

}); 

}]; 如果你想反向地理硬編碼對座標的深灰色 -

初始化CLLocation的位置,你的緯度和經度:

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

我也想指出,你仍然可以使用MKReverseGeocoder。它可能會在未來的iOS更新中被刪除。

+0

Got it!謝謝。 – Christoffer

+1

複製粘貼:http://stackoverflow.com/questions/11832150/mkreversegeocoder-deprecated-in-ios-5 – Rushi