限制是我只能将位置信息存储为字符串。
我希望使用coreLocation获取当前位置,然后将其转换为字符串,以便将其存储在数据库中。稍后,我希望从数据库中获取位置信息(字符串格式),并在地图上显示该位置。那么我该如何实现它呢?我需要将coreLocation的哪些信息存储为字符串?只有纬度和经度就足够了吗?那么,如何使用字符串构建coreLocation,以便在地图上显示它呢?谢谢!
发布于 2013-07-03 09:38:28
如果CoreLocation数据的最终用途是地图应用程序,那么只需要纬度和经度就足够了。
按如下方式使用NSValueTransformer:
@interface CLLocationToStringTransformer : NSValueTransformer
@end
@implementation CLLocationToStringTransformer
+ (BOOL) allowsReverseTransformation
{ return YES; }
+ (Class) transformedValueClass
{ return [NSString class]; }
- (id) transformedValue: (id) value
{ CLLocation *location = (CLLocation *) value;
return [NSString stringWithFormat: "%@ %@",
theLocation.coordinate.latitude,
theLocation.coordinate.longitude]; }
- (id)reverseTransformedValue:(id)value
{ NSString *string = (NSString *) value;
NSArray *parts = [string componentsSeparatedByString: @" "];
return [[CLLocation alloc] initWithLatitude: [parts[0] doubleValue]
longitude: [parts[1] doubleValue]]; }
@end发布于 2013-07-03 10:36:26
是的,保存纬度/经度是这里的关键。如果您尝试保存一个地址字符串,那么以后在地图上绘制将非常容易出错。
您可以只使用一个字符串,即纬度后跟逗号,然后是经度。当您稍后从数据库中获得此字符串时,只需用逗号拆分字符串即可。然后,您可以使用这些值作为纬度和经度来创建CLLocation对象或您需要的任何对象(MKAnnotation?)...
希望这能有所帮助。
https://stackoverflow.com/questions/17437825
复制相似问题