MKReverseGeocoder 已弃用

Posted

技术标签:

【中文标题】MKReverseGeocoder 已弃用【英文标题】:MKReverseGeocoder deprecated 【发布时间】:2012-11-12 09:13:25 【问题描述】:

我刚刚看到 MKReverseGeocoder 已被弃用。问题是,如何以最简单的方式更改为 CLGeocoder?很抱歉源代码太长(我认为你认为它很简单,但我对此很陌生)。这是我之前得到的……

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;
    

感谢一千次!

【问题讨论】:

【参考方案1】:

可能是这样的?

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 更新中被删除。

【讨论】:

复制粘贴:***.com/questions/11832150/…

以上是关于MKReverseGeocoder 已弃用的主要内容,如果未能解决你的问题,请参考以下文章

已弃用:指令 'allow_url_include' 在第 0 行的 Unknown 中已弃用

Modernizr.load 已弃用。 Yepnope.js 已弃用。怎么办?

UIAlertView 已弃用:首先在 iOS 9.0 中弃用 - UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用

DTD 是不是已弃用?

macOS 上的 OpenGL 是不是已弃用?

已弃用:each() 函数已弃用。 C:\xampp\htdocs\phprojekt\library\Zend\Cache\Backend.php 在第 66 行 [重复]