iphone - MKReverseGeocoder 已弃用

标签 iphone ios6 mapkit cllocationmanager clgeocoder

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

商店位置.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

商店位置.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;
    }

感谢一千次!

最佳答案

也许像这样?

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。不过, future 的 iOS 更新可能会删除它。

关于iphone - MKReverseGeocoder 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340932/

相关文章:

iphone - MapKit iPhone 开发 - 向 map 添加注释 - iOS SDK

iPhone UIWebView - 调用 loadHTMLString :baseURL: a second time doesn't do anything?

iphone - iPhone App 项目的维基百科页面解析器

iphone - 除了 UIWebView 之外,Cocoa 中还有 HTML 包装器吗?

swift - 使用 Mapkit 对聚类注释设置限制

ios - 是否可以强制 MapKit 在不聚类的情况下显示所有注释?

iphone - 在 Quartz 中从椭圆中心绘制径向线

ios - 在 Storyboard iOS 中隐藏导航栏的后退按钮

ios - 在应用内购买 开发者需要采取行动 状态

ios6 - SK产品本地化不同语言的描述