2013-07-03 62 views
0

限制是我只能將位置信息存儲爲字符串。
我希望使用coreLocation獲取當前位置,然後將其轉換爲字符串,以便將其存儲在數據庫中。稍後,我希望從數據庫中獲取位置信息(字符串格式)並在地圖上顯示該位置。那我該如何執行它?我需要將哪些coreLocation信息存儲爲字符串?只有經度和緯度才足夠?那麼如何使用字符串來構建coreLocation,以便我可以在地圖上顯示它?謝謝!以字符串形式存儲位置,從字符串中取回位置以顯示在地圖上。 iOS

回答

1

如果您最終將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 
+1

這是更正版本的transformedValue:方法: - (id)transformedValue:(id)value { CLLocation * location =(CLLocation *)value; return [NSString stringWithFormat:@「%f%f」, location.coordinate.latitude, location.coordinate.longitude]; } – scrat84

1

是,節省經/緯度是這裏的關鍵。如果您嘗試保存地址字符串,那麼稍後會錯誤地將其繪製回地圖上。

您可以製作一個單一的字符串,其緯度後跟逗號,然後是經度。當你稍後從數據庫中取回這個字符串時,就用逗號分隔字符串。然後,您可以使用這些值作爲經緯度來創建一個CLLocation對象或任何您需要的對象(MKAnnotation?)...

希望有所幫助。

相關問題