0
我想在每次按下「Get My」時,將緯度/經度座標保存在「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
太謝謝你了!我嘗試了NSDictionary的方式,但得到這個誹謗錯誤浮標長= [[locDict objectForKey:@「Longitude」] floatValue];錯誤是「預期的標識符或'('」和'long float'是無效的。我是objective-c的新手,甚至不知道我把它放在我的文件中的位置!要求你添加代碼太多我的代碼發佈在上面?如果沒有,這是好的,謝謝朋友PS。Plist文件方法,如果它更容易,如果罰款。 – Nicoll
在你的代碼中,你想將它保存在'locationManager:didUpdateToLocation:fromLocation:'方法。回答保存爲.plist。 –
是的,我在YouTube上找到了一個教程,發現我不得不使用locationManager,對不起,我是一個新手朋友,再次感謝!我等待你的更新。) – Nicoll