2012-06-15 2 views
1

내 앱 내부에서 핵심 위치 프레임 워크를 사용하고 있습니다. 앱이 처음 시작될 때 didUpdateToLocation 메서드가 두 번 호출됩니다. 위치 관리자 인스턴스를 할당하고 시작하고 startUpdatingLocation을 호출했습니다. 그리고 didUpdateToLocation에서 나는이 함수의 다중 호출을 피할 수 있도록 서버에 현재 위도와 경도를 보내는 다른 함수를 호출하고 있습니까?동일한 기능을 여러 번 호출하지 않으려면 어떻게해야합니까?

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.locationManager = [[CLLocationManager alloc] init]; 
[self.mapView setShowsUserLocation:YES]; 
if([CLLocationManager locationServicesEnabled]) 
{ 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation]; 
    locationManager.distanceFilter = 1000.0f; 

else 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"GPS Disabled"   message:@"Enable GPS settings to allow the application to search for your current location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
} 
} 

    //Here is didUpdateToLocation method 

-(void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
    { 
    self.lat = newLocation.coordinate.latitude; 
    self.longi = newLocation.coordinate.longitude; 

    NSLog(@"Updated Location : lat--%f long--%f",self.lat,self.longi); 
    [self updateUserLocation]; 



    } 

    //in this function i am sending the latitude and longitude to server. 
-(void)updateUserLocation 
{ 

    NSString *urlString = [[NSString stringWithFormat:@"http://appifylabs.com/tracemybuddies/index.php/iphone/updateUserLocation/?userId=1&latitude=%@&longitude=%@",self.lat,self.longi] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

[conn url_Connection:urlString withActivityView:self.activityView  withParentView:self.view]; 
    } 
+0

일단 업데이트 메서드에서 위치를 얻은 경우 업데이트를 중지하는 stopUpdating 메서드를 호출하십시오. – vishy

+0

여기에 코드를 입력하십시오.이 코드는 명확하게 이해할 수 있습니다 .. – Abhishek

+0

내가 필요하기 때문에이 작업을 수행 할 수 없습니다. 현재 위치가 내 앱 내부에서 업데이트되고 현재 위치가 업데이트 될 때마다 서버에 업데이트 된 위도와 경도를 전송합니다. 이 함수는 앱이 처음 시작할 때만 두 번 호출됩니다. 그렇지 않으면 현재 위치가 업데이트 될 때 한 번만 호출됩니다. – iosuser

답변

1

inside didUpdateToLocation, oldLocation이 NULL로 수신되면 함수에서 복귀합니다. 대리자를 처음 호출 할 때 NULL이됩니다.

둘째, currentTime과 newLocation.timeStamp 사이의 시간 스탬프 차이를 사용할 수 있습니다. 수용 가능한 한계치와 맞지 않으면 기능에서 복귀하십시오.

+0

oldLocation이 NULL 인 경우 didUpdateToLocation 메서드를 확인한 다음 function.its에서 반환하여 내 문제를 해결합니다. – iosuser

0

특정 시간 간격이 경과 한 경우에만 서버를 호출해야합니까?

int now = (int)[[NSDate date] timeIntervalSince1970]; 

if(now - self.lastUpdate > INTERVAL_BETWEEN_TWO_UPDATES) { 
    [self callServeur]; 
    self.lastUpdate = now; 
} 
관련 문제