0

MapViewのボタンタップでは、次のアクションを実行します。

- (IBAction)locate:(id)sender event:(UIEvent*)event {
    DLog(@"");
    if ([self.mapView respondsToSelector:@selector(userTrackingMode)]) {
        [self.mapView setUserTrackingMode:MKUserTrackingModeNone];
    }
    [self.mapView setShowsUserLocation:NO];
    [self.mapView setShowsUserLocation:YES];
}

を確認しuserLocation pin、次のメソッドを呼び出してマップの位置を変更します。

- (void) resizeRegionToFitAllPins:(BOOL)includeUserLocation animated:(BOOL)animated {
    if ([self.annotations count] == 1) {
        NSObject<MKAnnotation> *annotation = [self.annotations objectAtIndex:0];
        BOOL isUserLocation = [annotation isKindOfClass:MKUserLocation.class];
        if ((includeUserLocation && isUserLocation) ||
            isUserLocation == NO) {
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = annotation.coordinate.latitude;
            coordinate.longitude = annotation.coordinate.longitude;
            [self setRegion:MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.001f, 0.001f)) animated:animated];
        }
    }
}

私の問題は、マップがユーザーの位置を永遠に (ループで) 更新していることです。ユーザーの位置情報をアクティブ化した後、マップを使用することはできません。ユーザーの位置の更新を停止し、ユーザーがマップを使用できるようにするには何が必要ですか?

メソッドmapView:didUpdateUserLocation:は何度も呼び出されます。をMKMapViewDelegateインターフェイスに配置し、self.mapView.delegateを self に設定し、mapViewWillStartLocatingUser:andを呼び出しmapViewDidStopLocatingUser:のみで設定します。Dlog(@"")mapViewDidStopLocatingUser:呼び出されません。

- (void)mapView:(MKMapView *)map didUpdateUserLocation:(MKUserLocation *)userLocation {
    DLog(@"Resizing...");
    [self.mapView resizeRegionToFitAllPins:YES animated:YES];
    }
}

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}

- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}
4

3 に答える 3

0

このメソッドを使用mapView:didUpdateUserLocation:して、何度も呼び出されるメソッドを回避できます。

[self.locationManager stopUpdatingLocation];

位置情報の更新を停止する前に、次のことを覚えておいてください。

self.locationManager.delegate = self;

位置情報の更新を停止したら、次のことを覚えておいてください。

self.locationManager.delegate = nil;
于 2015-05-27T17:09:31.340 に答える
0

あなたは言った人です[self.mapView setShowsUserLocation:YES];。その状況が続いている限り、マップ ビューは引き続きユーザーの位置を追跡します。追跡を停止する場合は、マップ ビューに追跡を停止するように指示します。

ただし、マップ ビューのuserTrackingMode(aMKUserTrackingMode値) を使用して追跡動作を変更できます。

于 2015-05-27T16:46:40.153 に答える
0

想定どおりに動作しています。

位置情報の更新は、デバイスの移動に合わせて継続的に行われることになっています (ただし、位置情報の更新が停止し、精度が低い場合、更新の数は遅くなるか停止します)。

ただし、行間を読むと、次のように言うため、それが実際にあなたの問題であるようには思えません。

"It is impossible to use the map after activating the user's location".

はい、可能です。それが不可能であるという事実は、問題の原因が継続的な更新ではなく、それらの継続的な更新に対処するためのコードの設計方法にあることを意味します。マップを継続的に更新することは、非常に一般的なシナリオです。

あなたはそれらをする必要があります:

  • すべてのコードと、継続的な更新に対処するためにプログラムを正しく設計する方法を決定するために何をしたいのかを示す新しい質問を投稿してください。

  • コードを追加してresizeRegionToFitAllPins、ピンを追加または削除するたびに、または場所が特定の距離を変更した場合に一度だけ呼び出されるようにします。didUpdateLocationそれが問題の原因である場合、毎回呼び出されるわけではありません。

于 2015-05-27T17:51:05.537 に答える