2013-01-10 40 views
9

Tôi đang cố gắng mã hóa các chú thích trên bản đồ, nhưng tôi đọc rằng tôi không thể mã hóa các biến CLLocationcoordinate2D. Có ai biết làm thế nào tôi có thể giải quyết điều này? Đây là một số mã.Mã hóa CLLocationcoordinate2D

Đây là nơi tôi thả các chân:

- (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); 
         } 
        } 
       }]; 
} 

Đây là lớp học của tôi mà phù hợp với các protcol 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

gì là lỗi/hành vi bạn đang nhận được? –

+0

Bạn có ý gì khi "mã hóa CLLocationCoordinate2D"? Đến Base-64? Hay cái gì? –

+0

Vì vậy, tôi đang cố gắng mã hóa biến CLLocationCoordinate2D, nhưng khi tôi thử [aCoder encodeObject: _coordinate forKey: @ "Coord"]; Tôi nhận được lỗi Gửi 'CLLocationCoordinate2D' chu vi của loại không tương thích 'id' –

Trả lời

17

Chỉ cần mã hóa hai lĩnh vực của CLLocationCoordinate2D giá trị.

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

NSValue tuân thủ NSCoding. Bạn có thể hộp biến CLLocationcoordinate2D của bạn trong một đối tượng NSValue:

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

Tốt nhất, đáng để thêm rằng bạn phải thêm #import "MapKit/MapKit.h" (và khung MapKit). – KlimczakM

+3

Khi tôi cố gắng mã hóa CLLocationCoordinate2D, nó tạo ra một lỗi "- [NSKeyedArchiver encodeValueOfObjCType: at:]: bộ lưu trữ này không thể mã hóa cấu trúc '". Nói cho tôi biết tôi đang làm gì sai ở đây. –

4

vĩ độ và kinh độ Các CLLocationCoordinate2D 's là loại CLLocationDegrees đó là, về cơ bản, một cách ưa thích của nói double.

Đây là cách bạn có thể mã hóa và giải mã chúng:

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

tôi quyết định để mã hóa sử dụng một tay cầm CLLocation sở hữu tình huống này, mà phù hợp với NSSecureCoding.

Nếu bạn cần phải chuyển đổi đến hoặc từ một CLLocationCoordinate2D:

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

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