В моем приложении я сравниваю два местоположения. Один - это фактическое местоположение, а другое - фиксированное. Я установил второе местоположение кнопкой. Когда определенное расстояние покрыто, мне подсказывают 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];
}
}
Спасибо за ваш ответ. Мне жаль говорить вам, что результаты все те же. Я изменил код в методе locationManager (void), но у меня нет эффекта для моего метода recordLocation. Какие-либо предложения? – A3O