首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKReverseGeocoder已弃用

MKReverseGeocoder已弃用
EN

Stack Overflow用户
提问于 2012-11-12 17:13:26
回答 1查看 2.5K关注 0票数 2

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

StoreLocation.h

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
    -(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;
    }

万分感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-12 17:27:46

也许像这样?

在iOS4之后的所有固件中都不推荐使用MKReverseGeocoder。这只是意味着它现在已经过时了,并且不赞成使用过时的类。改为使用CLGeocoder,如下所示:

代码语言:javascript
复制
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位置:

代码语言:javascript
复制
CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

我还想指出,您仍然可以使用MKReverseGeocoder。不过,它可能会随着未来的iOS更新而删除。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13340932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档