2011-04-18 128 views
0

我有一個應用程序,我必須檢查用戶的移動位置是否已更改或連續30秒?如果它在30秒內沒有改變,則用戶將被導航到另一個視圖,如果它已經改變,那麼用戶將會在30秒內收到一條消息,表明您的移動位置的座標發生了變化。Iphone核心位置

我在這裏使用這些代碼,但它什麼也不做......

-(void)time 
{ 

    for(int i = 0; i<= timeremaining ;i++) 
    { 
     if (new1 == old) 
     { 
      if(timeremaining == 1) 
      { 
       Timer90 *timerview = [[Timer90 alloc] initWithNibName:@"Timer90" bundle:nil]; 
       [self.navigationController pushViewController:timerview animated:YES]; 

      } 

     } 
     else 
     { 
      [NSTimer cancelPreviousPerformRequestsWithTarget:self selector:@selector(time) object:nil]; 
     } 

     timeleft.text = [NSString stringWithFormat:@"%d",timeremaining]; 

    } 
    timeremaining--; 

} 
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 

    new1 = newLocation; 
    old=oldLocation; 

    latitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude]; 
    longitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude]; 
    accuracy = [[NSString alloc] initWithFormat:@"%g",newLocation.horizontalAccuracy]; 

    if(oldLocation == NULL) 
    { 
     oldLocation == newLocation; 
    } 

} 

timer90是我view.Please幫助我的朋友的名字...任何幫助將不勝感激。

回答

0

我注意到兩件事情。可能是整個問題,或者只是其中的一部分。

號碼1.

if(oldLocation == NULL) 
{ 
    oldLocation == newLocation; 
} 

oldLocation == newLocation不會將兩者彼此相等(你需要一個等號)和您的分配參數指針指向同一個對象,而不是你的成員指針。例如,如果oldLocation點到對象A和newLocation點的didUpdateToLocation:fromLocation:開始到對象B,則:

new1 is set to point at Object B 
old is set to point at Object A 
if(Object A is NULL) 
    oldLocation and newLocation both point at Object B 

名new1的值和舊未設置爲在NULL的情況下相同的對象。這就是你在time

數2

檢查time要檢查是否new1old在同一個對象指向。如果兩個對象表示相同的位置,則不一定必要。在oldLocation爲空並且它們被設置在一起的情況下,該檢查可以通過...但是,CoreLocation更有可能在準確性變得更好時調用更新方法幾次,並且您將有兩個單獨的對象相同的經緯度位置。

您應該使用CLLocation getDistanceFromLocation:方法比較這兩個位置的鄰近程度。

希望有助於!

0

你可以做這樣的事情(將計數器和初始位置添加到你的屬性後),它會比較第一次更新後30秒後的位置,但我認爲如果你爲整個方法添加了年齡檢查所以你沒有比較一些非常古老的數據點。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
    if(self.counter==0){ 
    self.intialLocation=newLocation; 
    self.counter=1; 
    } 
NSTimeInterval age = [initialLocation.timestamp timeIntervalSinceNow]; 
     if(age>30) 
     { 
    self.counter=0; 
    if(newLocation.latitude==initialLocation.latitude&&newLocation.longitude==initialLocation.longitude){ 
    Timer90 *timerview = [[Timer90 alloc] initWithNibName:@"Timer90" bundle:nil]; 
    [self.navigationController pushViewController:timerview animated:YES]; 
    } 
    } 
    } 
+0

WD對distanceFromLocation的評論也是明智的,而不是我的確切比較 – 2011-04-18 14:24:35