2013-01-10 19 views
9

我想編碼爲地圖上的標註,但我看,我不能夠編碼CLLocationcoordinate2D變量。有誰知道我能如何解決這個問題?這是一些代碼。編碼一個CLLocationcoordinate2D

這就是我把引腳:

- (void)press:(UILongPressGestureRecognizer *)recognizer { 
    CGPoint touchPoint = [recognizer locationInView:_worldView]; 
    CLLocationCoordinate2D touchMapCoordinate = [_worldView convertPoint:touchPoint toCoordinateFromView:_worldView]; 

    geocoder = [[CLGeocoder alloc]init]; 
    CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate 
                altitude:CLLocationDistanceMax 
              horizontalAccuracy:kCLLocationAccuracyBest 
              verticalAccuracy:kCLLocationAccuracyBest 
                timestamp:[NSDate date]]; 
    [geocoder reverseGeocodeLocation:location 
       completionHandler:^(NSArray *placemarks, NSError *error) { 
        //NSLog(@"reverseGeocoder:completionHandler: called"); 
        if (error) { 
         //NSLog(@"Geocoder failed with error: %@", error); 
        } else { 
         CLPlacemark *place = [placemarks objectAtIndex:0]; 
         geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]]; 
         if (UIGestureRecognizerStateBegan == [recognizer state]) { 
          value = [number intValue]; 
          number = [NSNumber numberWithInt:value + 1]; 
          _addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate 
                    title:geocodedAddress identifier:number]; 
          NSLog(@"The identifier is %@", number); 
          [[Data singleton].annotations addObject:_addressPin]; 
          [_worldView addAnnotation:_addressPin]; 
          NSLog(@"The number of pins in the annotation array is: %u",[Data singleton].annotations.count); 
         } 
        } 
       }]; 
} 

這裏是我的類,它符合MKAnnotation協議型:

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

@interface MapPoint : NSObject <MKAnnotation, NSCoding> 
{ 
} 

- (id)initWithAddress:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate 
     title:(NSString *)t 
     identifier:(NSNumber *)ident; 


//This is a required property from MKAnnotation 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

//This is an optional property from MKAnnotataion 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, readonly, copy) NSString *subtitle; 
@property (nonatomic) BOOL animatesDrop; 
@property (nonatomic) BOOL canShowCallout; 

@property (copy) NSString *address; 
@property (copy) NSNumber *identifier; 
@property (nonatomic, copy) NSString *imageKey; 
@property (nonatomic, copy) UIImage *image; 

@end 

import "MapPoint.h" 

@implementation MapPoint 

@synthesize title, subtitle, animatesDrop, canShowCallout, imageKey, image; 
@synthesize address = _address, coordinate = _coordinate, identifier = _indentifier; 

-(id)initWithAddress:(NSString *)address 
     coordinate:(CLLocationCoordinate2D)coordinate 
      title:(NSString *)t 
     identifier:(NSNumber *)ident { 
    self = [super init]; 

    if (self) { 
     _address = [address copy]; 
     _coordinate = coordinate; 
     _indentifier = ident; 

     [self setTitle:t]; 

     NSDate *theDate = [NSDate date]; 

     subtitle = [NSDateFormatter localizedStringFromDate:theDate 
               dateStyle:NSDateFormatterShortStyle 
               timeStyle:NSDateFormatterShortStyle]; 
     } 
    return self; 
} 

- (void)encodeWithCoder:(NSCoder *)aCoder { 

    [aCoder encodeObject:_address forKey:@"address"]; 


    [aCoder encodeObject:title forKey:@"title"]; 
    [aCoder encodeObject:_indentifier forKey:@"identifier"]; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super init]; 
    if (self) { 
     [self setAddress:[aDecoder decodeObjectForKey:@"address"]]; 

    } 
    return self; 
} 

@end

+0

什麼是錯誤/行爲你得到? –

+0

你是什麼意思「編碼CLLocationCoordinate2D」?到Base-64?或者是什麼? – 2013-01-10 23:08:48

+0

所以我想編碼CLLocationCoordinate2D變量,但是當我嘗試[aCoder encodeObject:_coordinate forKey:@「Coord」];我得到的錯誤發送不兼容的類型「ID」 –

回答

17

剛剛編碼的兩個領域值爲CLLocationCoordinate2D

[aCoder encodeDouble:_coordinate.latitude forKey:@"latitude"]; 
[aCoder encodeDouble:_coordinate.longitude forKey:@"longitude"]; 
9

NSValue符合NSCoding標準。您可以框在NSValue對象的CLLocationcoordinate2D變量:

[coder encodeObject:[NSValue valueWithMKCoordinate:coordinate] forKey:@"coordinate"] 
+0

尼斯一個,價值的「CLLocationCoordinate2D」周補充說,你必須添加#進口「MapKit/MapKit.h」(和MapKit框架)。 – KlimczakM

+3

當我試圖編碼CLLocationCoordinate2D,它產生的誤差「 - [的NSKeyedArchiver encodeValueOfObjCType:在:]:這個歸檔不能編碼結構」。告訴我我在這裏做錯了什麼。 –

4

CLLocationCoordinate2D的緯度和經度CLLocationDegrees類型,其本質上是說double的一個奇特的方式。

這是你可以編碼和解碼他們:

NSString *const kPinCoordinateLatitudeKey = @"kPinCoordinateLatitudeKey"; 
NSString *const kPinCoordinateLongitudeKey = @"kPinCoordinateLongitudeKey"; 

- (void)encodeWithCoder:(NSCoder *)encoder 
{ 
    [encoder encodeDouble:self.coordinate.latitude forKey:kPinCoordinateLatitudeKey]; 
    [encoder encodeDouble:self.coordinate.longitude forKey:kPinCoordinateLongitudeKey]; 
} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    if((self = [super init])) { 
     CLLocationDegrees latitude = [decoder decodeDoubleForKey:kPinCoordinateLatitudeKey]; 
     CLLocationDegrees longitude = [decoder decodeDoubleForKey:kPinCoordinateLongitudeKey]; 
     _coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
    } 
    return self; 
} 
2

我決定編碼使用CLLocation財產處理這種情況,這符合NSSecureCoding

如果您需要轉換或從CLLocationCoordinate2D:

// Coordinate to Location 
CLLocationCoordinate2D coord; 
CLLocation *loc = [[CLLocation alloc] initWithLatitude:coord.latitude 
              longitude:coord.longitude]; 

// Location to Coordinate 
CLLocationCoordinate2D coord = loc.coordinate;