我想将纬度/经度坐标保存在名为“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
答案 0 :(得分:1)
您可以创建一个您的应用知道如何阅读的NSString。 E.g:
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];
}