2

私のアプリでは、2 つの場所を比較しています。1 つは実際の場所で、もう 1 つは固定された場所です。ボタンで2番目の位置を設定します。特定の距離がカバーされると、alertView が表示されます。停止ボタンをタップすると、locationmanager が停止します。問題は、もう一度開始したいときに、アプリが新しい場所ではなく、停止ボタンをタップした場所を使用することです。NSTimeInterval を使用してキャッシュを除外しようとしましたが、まだ最後の「既知の」位置を取得しています。助けてください。

これが私のコードです(2つの方法で)

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

}

「固定」位置を更新する2番目のもの:

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

}
4

3 に答える 3

1

OS は時間とエネルギーを節約しているだけなので、キャッシュされた場所を取得します。に独自のフィルターを実装する必要があります-didUpdateToLocation。このメソッドは、 を呼び出すまで毎秒呼び出されます-stopUpdatingLocation。ロケーションのタイムスタンプや精度に満足するまで、このメソッドを呼び出せるようにコードを作成します。実行時間が長いほど、位置はより正確になります。また、GPS の稼働率が高くなり、稼働時間が長すぎるとバッテリーに悪影響を与える可能性があることも意味します。

于 2013-03-26T15:11:44.047 に答える
0

私のアプリの構造のため(2つの方法で場所が必要です)、 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];

}

于 2013-03-12T15:23:47.697 に答える
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
    }
}
于 2013-03-12T12:05:38.567 に答える