简体   繁体   English

编码CLLocationcoordinate2D

[英]Encoding a CLLocationcoordinate2D

I am trying to encode annotations that are on a map, but I read that I am not able to encode CLLocationcoordinate2D variables. 我正在尝试编码地图上的注释,但我读到我无法对CLLocationcoordinate2D变量进行编码。 Does anyone know how I can solve this? 有谁知道我怎么解决这个问题? Here is some code. 这是一些代码。

This is where I drop the pins: 这是我放下引脚的地方:

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

Here is my class that conforms to the MKAnnotation protcol: 这是我的类符合MKAnnotation protcol:

#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 @结束

Just encode the two fields of the CLLocationCoordinate2D value. 只需编码CLLocationCoordinate2D值的两个字段。

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

NSValue is NSCoding compliant. NSValue符合NSCoding标准。 You can box your CLLocationcoordinate2D variable in an NSValue object: 您可以在NSValue对象中打包CLLocationcoordinate2D变量:

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

The CLLocationCoordinate2D 's latitude and longitude are of type CLLocationDegrees which is, essentially, a fancy way of saying double . CLLocationCoordinate2D的纬度和经度属于CLLocationDegrees类型,这实际上是一种说double的奇特方式。

This is how you can encode and decode them: 这是你如何编码和解码它们:

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;
}

I decided to encode use a CLLocation property handle this situation, which conforms to NSSecureCoding . 我决定编码使用CLLocation属性处理这种情况,这符合NSSecureCoding

If you need to convert to or from a CLLocationCoordinate2D: 如果您需要转换为CLLocationCoordinate2D或从CLLocationCoordinate2D转换:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM