gpt4 book ai didi

objective-c - 编码 CLLocationcoordinate2D

转载 作者:太空狗 更新时间:2023-10-30 03:24:56 26 4
gpt4 key购买 nike

我正在尝试对 map 上的注释进行编码,但我了解到我无法对 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 协议(protocol)的类:

#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 值的两个字段进行编码。

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

关于objective-c - 编码 CLLocationcoordinate2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14268766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com