2013-01-10 4 views
9

Sto tentando di codificare le annotazioni che sono su una mappa, ma ho letto che non sono in grado di codificare le variabili CLLocationcoordinate2D. Qualcuno sa come posso risolvere questo? Ecco un codice.Encoding a CLLocationcoordinate2D

Questo è dove mi calo i perni:

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

Qui è la mia classe che è conforme alla 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

Qual è l'errore/comportamento che si stanno ottenendo? –

+0

Cosa intendi con "codifica di CLLocationCoordinate2D"? Alla base 64? O cosa? –

+0

Quindi sto provando a codificare la variabile CLLocationCoordinate2D, ma quando provo [aCoder encodeObject: _coordinate forKey: @ "Coord"]; Ottengo l'errore Invio del perimetro 'CLLocationCoordinate2D' di tipo incompatibile 'id' –

risposta

17

Basta codificare i due campi della Valore CLLocationCoordinate2D.

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

NSValue è compatibile con NSCoding. Si può box la variabile CLLocationcoordinate2D in un oggetto NSValue:

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

Bello, vale la pena aggiungere che devi aggiungere #import "MapKit/MapKit.h" (e framework MapKit). – KlimczakM

+3

Quando ho provato a codificare CLLocationCoordinate2D, ha prodotto un errore "- [NSKeyedArchiver encodeValueOfObjCType: at:]: questo archiver non può codificare le strutture" ". Dimmi cosa sto facendo male qui. –

4

latitudine e longitudine 's La CLLocationCoordinate2D sono di tipo CLLocationDegrees che è, essenzialmente, un modo elegante per dire double.

In questo modo è possibile codificare e decodificare:

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

ho deciso di codificare l'uso di una maniglia CLLocation proprietà questa situazione, che è conforme alla NSSecureCoding.

Se avete bisogno di convertire da o verso un CLLocationCoordinate2D:

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

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