[英]How can I save coordinates to a text file?
我希望每次按“獲取我的位置”時,將緯度/經度坐標保存在“ var / mobile / Documents”文件夾中的文本文件“ Location.txt”(首先必須創建此文件)中按鈕,每次覆蓋舊坐標。 這可能嗎? 有人可以給我舉例說明如何做到這一點。 謝謝。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = (id)self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_LongitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
@end
您可以創建一個您的應用程序知道如何讀取的NSString。 例如:
NSString *newLocString = [NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.longitude, currentLocation.coordinate.latitude];
NSString *path = //File Path.txt
NSError *error = nil;
// Save string and check for error
if (![newLocStrin writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"An Error occurred: %@", error.localizedDescription);
}
然后像這樣閱讀:
NSString *path = //File Path.txt
NSError *error = nil;
NSString *location = [NSString stringWithContentsOfFile:path usedEncoding:NSUTF8StringEncoding error:&error];
if (!location) {
NSLog(@"Error reading location file: %@", error.localizedDescription);
}
else {
// Have read the string so now format it back
NSString *longitude = nil;
NSString *latitude = nil;
NSScanner *scanner = [NSScanner scannerWithString:location];
[scanner scanUpToString:@"," intoString:&longitide];
latitude = [location stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@,",longitude] withString:@""];
NSLog(@"Location equals %f,%f", longitude.floatValue, latitude.floatValue);
}
使用NSDictionary並將其保存到NSUserDefaults或保存為.plist文件可能會更容易:
NSDictionary *dict = @{@"Longitude":@(currentLocation.coordinate.longitude), @"Latitude":@(currentLocation.coordinate.latitude)};
然后為用戶默認值:
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Location"];
// To read...
NSDictionary *locDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"Location"];
float longitude = [[locDict objectForKey:@"Longitude"] floatValue];
// e.t.c
要寫入.plist文件,請使用NSDictionary的writeToFile: atomically:
類似於上述NSString方法的方法。
希望這足以完成工作!
編輯:所有文件寫入方法都將覆蓋文件的舊版本。
編輯2:將plist文件保存在您的代碼中:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_LongitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSDictionary *locDict = @{@"Long":@(currentLocation.coordinate.longitude),@"Lat":@(currentLocation.coordinate.latitude)};
// Probably want to save the file in the Application Support directory
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"location.plist"];
if (![locDict writeToFile:path atomically:YES]) {
NSLog(@"An error occurred.");
}
// Or save to NSUserDefaults
[[NSUserDefaults standardUserDefualts] setObject:locDict forKey:@"Location"];
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.