为什么我总是在ios中获得缓存位置

时间:2013-03-12 11:45:31

标签: ios objective-c cllocationmanager nstimeinterval

在我的应用程序中,我正在比较两个位置。一个是实际位置,另一个是固定位置。我用按钮设置了第二个位置。当覆盖一定距离时,我会通过alertView提示。通过点击停止按钮,位置管理器停止。问题是当我想再次启动时,应用程序使用了我点击停止按钮的位置,而不是新的按钮。我试图通过使用NSTimeInterval来规则缓存,但我仍然得到最后的“已知”位置。请帮忙。

这是我的代码(用两种方法)

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

}

3 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

由于我的应用程序的结构(需要两个方法中的位置),使用teh abs()语句的NSTimeinterval不起作用。从提供的答案来看,我会更多地了解我,但我的解决方案是我的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];

}

答案 2 :(得分:0)

您获得了缓存位置,因为操作系统只是节省时间和精力。您需要在-didUpdateToLocation中实现自己的过滤器。在您致电-stopUpdatingLocation之前,此方法将每秒调用一次。我会围绕让这个方法被调用来构建你的代码,直到你对位置时间戳和/或准确性感到满意为止。它运行的时间越长,位置就越准确。这也意味着如果运行时间过长,GPS会运行得更多并且会对电池产生负面影响。