2012-12-27 40 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