2012-12-27 5 views
1

내 응용 프로그램 내에서 모든 사용자 위치를 표시하는지도를 설계 할 수 있도록 저장소에 서버에 사용자 위치의 GPS 좌표를 게시하려고합니다. HTTP에서 get 및 사용자 지정 PHP API를 사용하여 응용 프로그램에서 db로 전달되는 데이터를 처리합니다. 문제는 내가 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을 곱하여 정수로 변환 할 수 있으며 서버 측에서 같은 값으로 나눕니다. 어떻게하면 더 편할까요? –

관련 문제