0

我目前有一个应用程序,当我在地图上放置注释时,它会显示标题和副标题,副标题显示纬度/经度/半径信息。

 -(NSString *)title{
    return @"Saved position";
}

   - (NSString *)subtitle {
        return [NSString stringWithFormat: @"Lat: %.4F, Lon: %.4F, Rad: %.1fm",
        self.coordinate.latitude, self.coordinate.longitude, self.radius];
    }

    -(id)initWithCoordinate:(CLLocationCoordinate2D) coor{
        self.coordinate=coor;
        NSLog(@"%f,%f",coor.latitude,coor.longitude);
        return self;
    }

我想更改字幕行以显示地址。我知道这是使用 CLGeocoder 的反向地理编码,但是我不确定如何正确执行此操作。我在网上查看了许多示例,并查看了苹果开发网站,但它有点压倒性。任何帮助都非常感谢这位新手程序员。

4

1 回答 1

0

我已经解决了这个问题,并想分享我是如何做到的:

RegionAnnotation.h

#import <MapKit/MapKit.h>
#import <CoreFoundation/CoreFoundation.h>

@interface RegionAnnotation : NSObject <MKAnnotation, CLLocationManagerDelegate>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *subtitle;
@property (nonatomic, readonly, copy) NSString *title;

//Reverse Geocoding
@property(nonatomic, readonly) CLPlacemark *administrativeArea;//The state
@property(nonatomic, readonly) CLPlacemark *locality; //The city 
@property(nonatomic, readonly) CLPlacemark *subThoroughfare;//Street number
@property(nonatomic, readonly) CLPlacemark *thoroughfare;//Street address

@end

RegionAnnotation.m

#import "RegionAnnotation.h"
#import <AddressBookUI/AddressBookUI.h>

@implementation RegionAnnotation

@synthesize coordinate = _coordinate, subtitle = _subtitle, title = _title, administrativeArea = _administrativeArea, location = _location, locality = _locality, subThoroughfare = _subThoroughfare, thoroughfare = _thoroughfare;

- (id)init
{
    self = [super init];

    return self;    
}

- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    _coordinate = newCoordinate;
}

- (NSString *)title {
    return @"Monitored Region";
}

- (void) setSubtitle:(NSString *)newSubtitle
{
    _subtitle = newSubtitle;
}

-(void)reverseGeocodeLocation:(CLPlacemark *)placemark
{

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    CLLocationCoordinate2D coord = self.coordinate;

    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];

        NSLog(@"%@ %@ %@ %@",
              placemark.subThoroughfare,
              placemark.thoroughfare,
              placemark.locality, 
              placemark.administrativeArea);
    }];
}


- (NSString *)getAddress {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude];
    NSString *geoAddress = [[NSString alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
        if (error){
            NSLog(@"Geocode failed with error: %@", error);
//            [self displayError:error];
            return;
        }
        NSLog(@"getAddress Received placemarks: %@", placemarks);
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
         // use the AddressBook framework to create an address dictionary
         NSString *addressString = ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO);        
        _subtitle = addressString;
    }];

    return geoAddress;

}


@end

我目前让用户长时间推动以在地图视图上创建注释。现在,当用户通过按下它来选择引脚时,它会显示物理地址而不是经度和纬度。

我希望这对所有一直在寻找方法的人有所帮助。

于 2012-08-14T16:43:45.863 回答