【问题标题】:iOS trying to post location updates to a serveriOS 尝试将位置更新发布到服务器
【发布时间】:2012-12-27 19:29:03
【问题描述】:

在我的应用程序中,我试图将用户位置的 gps 坐标发布到服务器进行存储,以便我最终可以设计一个显示所有用户位置的地图。我正在使用 HTTP get 和自定义 PHP API 来处理从应用程序传递到数据库的数据。我遇到的问题是,每次调用 didUpdateLocations 时,我都会更新服务器。它有时有效,但有时我的查询字符串说有一个未定义的变量,并且空白数据正在发布到他的数据库中。为什么有时它是未定义的,有时不是?另外,有没有更好的方法来处理数据传递?我打算使用 ASIHTTPRequest 但我使用的是 ARC,所以这对我没有帮助。

代码:

- (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];
}

【问题讨论】:

    标签: php ios coordinates cllocationmanager http-get


    【解决方案1】:
    didUpdateLocations
    

    可以在您实际丢失位置时调用:进入电梯/建筑物。这就是为什么有时是空的。

    在发送服务器之前,我会检查并验证位置值。

    【讨论】:

    • 好吧,还有,将它们转换为字符串的最佳方法是还是有更好的方法?
    • 我记得它是一个浮点数,它计算小数点后的 5 位数字。所以我会格式化为像“%1.5f”这样的字符串,我会把它发送到服务器,如果是一个新的、经过验证的值,或者是一个“意味着”“位置丢失”的自定义值,比如“-200”或类似的东西.您可以通过乘以 100000 转换为 int 并在服务器端除以相同的值,这样对您来说更容易
    猜你喜欢
    • 2012-03-31
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多