2013-03-12 66 views
1

在我的應用程序中,我比較了兩個位置。一個是實際位置,另一個是固定位置。我用一個按鈕設置第二個位置。當一定距離被覆蓋時,我會提示一個alertView。通過點擊停止按鈕,位置管理器停止。問題是當我想再次開始時,應用程序使用的位置,我點擊停止按鈕,而不是一個新的。我試圖通過使用NSTimeInterval來排除緩存,但我仍然獲得最後一個「已知」位置。請幫忙。爲什麼我總是在ios中獲得緩存的位置

這是我的代碼(在兩種方法)

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

eventDate = newLocation.timestamp; 

NSLog (@"eventDate from locmanager %@", eventDate); 

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]; 
[formatter setGroupingSeparator:groupingSeparator]; 
[formatter setGroupingSize:3]; 
[formatter setAlwaysShowsDecimalSeparator:NO]; 
[formatter setUsesGroupingSeparator:YES]; 


//display latitude 
NSString *lat =[[NSString alloc]initWithFormat:@"%f", 
       newLocation.coordinate.latitude]; 
latitude.text=lat; 

//display longitude 
NSString *lon = [[NSString alloc]initWithFormat:@"%f", 
       newLocation.coordinate.longitude]; 
longitude.text=lon; 

//display accuracy 
accuracy.text = [formatter stringFromNumber:[NSNumber numberWithFloat:newLocation.horizontalAccuracy]]; 


double actLat = [[latitude text]doubleValue]; 
int latSeconds = actLat * 3600; 
int latDegrees = latSeconds/3600; 
latSeconds = abs(latSeconds % 3600); 
int latMinutes = latSeconds/60; 
latSeconds %= 60; 

NSString *latStringLocal = NSLocalizedString((actLat > 0) ? @"North" : @"South", @""); 

double actLon = [[longitude text]doubleValue]; 
int lonSeconds = actLon * 3600; 
int lonDegrees = lonSeconds/3600; 
lonSeconds = abs(lonSeconds % 3600); 
int lonMinutes = lonSeconds/60; 
lonSeconds %= 60; 

NSString *lonStringLocal = NSLocalizedString((actLon > 0) ? @"East" : @"West", @""); 

NSString *actPosWord = NSLocalizedString (@"WordActual", @""); 
NSString *actPosition = [[NSString alloc]initWithFormat:@"%@ %i° %i' %i\" %@" " " @"%i° %i' %i\" %@", actPosWord, latDegrees, latMinutes, latSeconds, latStringLocal,lonDegrees, lonMinutes, lonSeconds, lonStringLocal]; 

_actualPosition.text = actPosition; 

[self distanceFromLocation]; 

} 

,第二個刷新「固定」位置:

-(void)recordLocation{ 

NSDate* timeNow = [NSDate date]; 

NSLog (@"eventDate from recordLocation %@", eventDate); 
    if ([timeNow timeIntervalSinceDate:eventDate] > 2.0f) 

{ 

double valueLat = [[latitude text]doubleValue]; 
int latSeconds = valueLat * 3600; 
int latDegrees = latSeconds/3600; 
latSeconds = abs(latSeconds % 3600); 
int latMinutes = latSeconds/60; 
latSeconds %= 60; 

NSString *latStringLocal = NSLocalizedString((valueLat > 0) ? @"North" : @"South", @""); 

double valueLon = [[longitude text]doubleValue]; 
int lonSeconds = valueLon * 3600; 
int lonDegrees = lonSeconds/3600; 
lonSeconds = abs(lonSeconds % 3600); 
int lonMinutes = lonSeconds/60; 
lonSeconds %= 60; 

NSString *lonStringLocal = NSLocalizedString((valueLon > 0) ? @"East" : @"West", @""); 

tempPos = [[CLLocation alloc] initWithLatitude:valueLat longitude:valueLon]; 

NSString *watchedPosWord = NSLocalizedString (@"WordWatched", @""); 
NSString *recPosition = [[NSString alloc]initWithFormat:@"%@ %i° %i' %i\" %@" " " @"%i° %i' %i\" %@ \n", watchedPosWord, latDegrees, latMinutes, latSeconds, latStringLocal,lonDegrees, lonMinutes, lonSeconds, lonStringLocal]; 

_labelDegrees.text = recPosition; 

//create region for ovelay 

double areaWatched = [[watchedArea text] doubleValue]; 

MKCoordinateRegion region; 
region.center.latitude = valueLat; 
region.center.longitude = valueLon; 
region.span.latitudeDelta = 0.001f; 
region.span.longitudeDelta = 0.001f; 
region.center = tempPos.coordinate; 
[self.mapView setRegion:region animated:YES]; 

MKCircle *circle = [MKCircle circleWithCenterCoordinate:region.center radius:areaWatched]; 

[self.mapView addOverlay:circle]; 
} 

} 

回答

0

因爲我的應用程序的結構(需要在兩個方法的位置)NSTimeinterval與德ABS()聲明沒有工作。它告訴我更多關於我作爲提供的答案,但我的解決方案是在我的Activat方法中的NSTimer聲明。延遲0.1秒,它完美的工作。 任何有興趣,她是我的代碼:

- (IBAction)Activate:(UIButton *)sender { 
[self startUpdatingLocation]; 
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; 
i = 1; 
[NSTimer scheduledTimerWithTimeInterval:0.1 
           target:self 
           selector:@selector(recordLocation) 
           userInfo:nil 
           repeats:NO]; 

}

0

創建屬性,如@property (nonatomic, retain) CLLocation *currentLocation;並用它來存儲位置經理獲取的最後位置。 同樣重要的是覈實,如果位置管理器不發送緩存的位置,就像這樣:

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

    //if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session 
    NSDate *eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 

    if (abs(howRecent) < 15.0) 
    { 

     self.currentLocation = newLocation; 
     // do whatever you have to do 
    } 
} 
+0

感謝您的回覆。我很抱歉地告訴你,結果仍然是一樣的。我更改了(void)locationManager方法中的代碼,但它對我的recordLocation方法沒有影響。有什麼建議麼? – A3O 2013-03-12 12:38:09

0

你得到了緩存的位置,因爲OS只是節省了時間和精力。您需要在-didUpdateToLocation中實施您自己的過濾器。每秒調用一次該方法,直到您致電-stopUpdatingLocation。我會建立你的代碼,讓這個方法被調用,直到你對位置時間戳和/或精度感到滿意爲止。運行時間越長,位置就越精確。這也意味着GPS運行更多,如果運行時間過長,可能會對電池造成負面影響。

+0

謝謝你的回覆。我嘗試過不同的方式,但始終是最後一次緩存的位置,當應用程序重新啓動時的起點。我想出了一個解決方案(請參閱下面的答案)暫停0.1秒的方法。那時位置管理員已經更新了新的位置,現在一切正常。再次感謝。 – A3O 2013-03-27 10:14:50

相關問題