2014-11-15 64 views
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 

回答

1

您可以創建一個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文件,請使用類似於上述NSString方法的NSDictionary的writeToFile: atomically:方法。

希望這足以完成工作!

編輯:所有的文件寫入方法覆蓋舊版本的文件。

編輯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]; 
} 
+0

太謝謝你了!我嘗試了NSDictionary的方式,但得到這個誹謗錯誤浮標長= [[locDict objectForKey:@「Longitude」] floatValue];錯誤是「預期的標識符或'('」和'long float'是無效的。我是objective-c的新手,甚至不知道我把它放在我的文件中的位置!要求你添加代碼太多我的代碼發佈在上面?如果沒有,這是好的,謝謝朋友PS。Plist文件方法,如果它更容易,如果罰款。 – Nicoll

+0

在你的代碼中,你想將它保存在'locationManager:didUpdateToLocation:fromLocation:'方法。回答保存爲.plist。 –

+0

是的,我在YouTube上找到了一個教程,發現我不得不使用locationManager,對不起,我是一個新手朋友,再次感謝!我等待你的更新。) – Nicoll

相關問題