我刚看到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;
}
万分感谢!
答案 0 :(得分: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位置:
CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
我还想指出你仍然可以使用MKReverseGeocoder。它可能会在未来的iOS更新中删除。