限制是我只能將位置信息存儲爲字符串。
我希望使用coreLocation獲取當前位置,然後將其轉換爲字符串,以便將其存儲在數據庫中。稍後,我希望從數據庫中獲取位置信息(字符串格式)並在地圖上顯示該位置。那我該如何執行它?我需要將哪些coreLocation信息存儲爲字符串?只有經度和緯度才足夠?那麼如何使用字符串來構建coreLocation,以便我可以在地圖上顯示它?謝謝!以字符串形式存儲位置,從字符串中取回位置以顯示在地圖上。 iOS
0
A
回答
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
是,節省經/緯度是這裏的關鍵。如果您嘗試保存地址字符串,那麼稍後會錯誤地將其繪製回地圖上。
您可以製作一個單一的字符串,其緯度後跟逗號,然後是經度。當你稍後從數據庫中取回這個字符串時,就用逗號分隔字符串。然後,您可以使用這些值作爲經緯度來創建一個CLLocation對象或任何您需要的對象(MKAnnotation?)...
希望有所幫助。
相關問題
- 1. Bing Maps位置api:以字符串形式獲取響應
- 2. 在字符串中存儲字符的位置
- 3. 以SQL映像形式存儲並以64位字符串形式傳輸後的文件大小字符串
- 4. 如何在java中以字符串形式存儲超過50位的數字?
- 5. iOS - 字符串 - 在特定位置插入字符串
- 6. 字符在字符串中當前位置左側的位置
- 7. 從字符串獲取int位置PHP
- 8. 獲取索引位置從字符串
- 9. 子串在字符串中的位置
- 10. 返回方法中的位置字符串存儲
- 11. 如何獲取字符串中模式的字符位置?
- 12. 在谷歌地圖上的位置的字符串地址
- 13. 將字符串轉換爲uri以位圖顯示在ImageView中
- 14. 字符串的多邊形位置?
- 15. 按位置從字符串中獲取某些字符
- 16. 根據字符串位置在Google地圖中放置標記
- 17. 常量字符串將存儲在內存中的位置?
- 18. 字符串在鼠標上的位置?
- 19. 資源字符串位置
- 20. 解析位置字符串
- 21. 由字符位置掰開字符串
- 22. 在字符串中的指定位置獲取字符
- 23. 獲取字符串上的字符位置
- 24. 如何散列字符串以獲取陣列中的位置
- 25. python 2.7.6在字符串中打印字符串位置
- 26. 如何找到字符串放在字符串中的位置?
- 27. 在字符串中傳遞位置作爲字符串
- 28. 在字符串數組中的字符串的特定位置
- 29. 當唯一字符串位於2個不同位置時,從較長字符串獲取唯一字符串
- 30. 如何從字符串批量獲取字符的位置
這是更正版本的transformedValue:方法: - (id)transformedValue:(id)value { CLLocation * location =(CLLocation *)value; return [NSString stringWithFormat:@「%f%f」, location.coordinate.latitude, location.coordinate.longitude]; } – scrat84