2012-12-27 36 views
1

在我的應用程序中,我試圖將用戶位置的gps座標發佈到服務器進行存儲,以便我最終可以設計一個顯示所有用戶位置的地圖。我使用HTTP get和一個自定義PHP API來處理從應用程序傳遞到數據庫的數據。我遇到的問題是,每次didUpdateLocations被調用時,我都會更新服務器。它有時會起作用,但有時候我的查詢字符串表示存在一個未定義的變量,並且空白數據正在發佈到他的db中。爲什麼有時它不確定,有時不是?另外,是否有更好的方式來處理數據傳遞?我打算使用ASIHTTPRequest,但我使用ARC,所以這對我沒有任何幫助。iOS試圖將位置更新發布到服務器

代碼:

- (void)postLocationUpdateFor:(NSString *)deviceToken withDeviceID:(NSString*)deviceID withLatitude:(NSString *)latitude andLongitude:(NSString *)longitude { 

    NSString *apiURL = [NSString stringWithFormat:ServerApiURL2, deviceToken, deviceID, latitude, longitude]; 
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:apiURL]]; 
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@", strResult); 

} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 

    CLLocation *currentLocation = [locations lastObject]; 

    NSString *deviceToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"deviceToken"]; 
    NSString *latitude = [NSString localizedStringWithFormat:@"%f",currentLocation.coordinate.latitude]; 
    NSString *longitude = [NSString localizedStringWithFormat:@"%f",currentLocation.coordinate.longitude]; 

    NSLog(@"Entered new Location with the coordinates Latitude: %@ Longitude: %@", latitude, longitude); 

    [self postLocationUpdateFor:@"123" withDeviceID:deviceToken withLatitude:latitude andLongitude:longitude]; 
} 

回答

2
didUpdateLocations 

可實際上當你失去了你的位置被稱爲:進入一個電梯/建築。這就是爲什麼有時是空的。

我會在發送服務器之前檢查並驗證位置值。

+0

好吧,還有,將它們轉換爲字符串還是有更好的方法嗎? –

+0

這是一個浮點數,我記得它計數小數點後的5位數。所以我會格式化爲像「%1.5f」這樣的字符串,並且我會將它發送到服務器,如果是一個新的驗證值,或者是一個自定義值,這意味着「位置丟失」就像「-200」或類似的東西。你可以通過乘以100000來轉換爲int,並在服務器端用相同的值進行分割,就像你對easyer的方式一樣 – 2012-12-27 19:45:09