编码 CLLocationcoordinate2D

Posted

技术标签:

【中文标题】编码 CLLocationcoordinate2D【英文标题】:Encoding a CLLocationcoordinate2D 【发布时间】:2013-01-10 22:54:08 【问题描述】:

我正在尝试对地图上的注释进行编码,但我读到我无法对 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;

@结束

【问题讨论】:

您遇到的错误/行为是什么? “编码 CLLocationCoordinate2D”是什么意思?到 Base-64?还是什么? 所以我试图对 CLLocationCoordinate2D 变量进行编码,但是当我尝试 [aCoder encodeObject:_coordinate forKey:@"Coord"];我收到错误发送不兼容类型“id”的“CLLocationCoordinate2D”周长 【参考方案1】:

只需对CLLocationCoordinate2D 值的两个字段进行编码。

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

【讨论】:

【参考方案2】:

NSValue 符合 NSCoding 标准。您可以将 CLLocationcoordinate2D 变量装箱到 NSValue 对象中:

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

【讨论】:

不错,值得补充的是,您必须添加#import "MapKit/MapKit.h"(和 MapKit 框架)。 当我尝试对 CLLocationCoordinate2D 进行编码时,它产生了一个错误“-[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs'”。告诉我我在这里做错了什么。【参考方案3】:

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;

【讨论】:

【参考方案4】:

我决定编码使用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;

【讨论】:

以上是关于编码 CLLocationcoordinate2D的主要内容,如果未能解决你的问题,请参考以下文章

MKPolygon - 如何确定 CLLocationCoordinate2D 是不是在 CLLocationCoordinate2D 多边形中?

[CLLocationCoordinate2d]?没有名为 subscript 的成员

NSKeyedArchiver 因 CLLocationCoordinate2D 结构而失败。为啥?

使用未解析的标识符“CLLocationCoordinate2D”

在 Swift 3 中解析 JSON (CLLocationCoordinate2D)

CLLocationCoordinate2D 无法实例化