gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:18:53 25 4
gpt4 key购买 nike

在我的应用中,我正在比较两个位置。一个是实际位置,另一个是固定位置。我通过按钮设置第二个位置。当覆盖一定距离时,系统会提示我使用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];
}

}

最佳答案

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

关于ios - 为什么我总是在ios中获得缓存的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15360281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com