7

マップでユーザーを追跡する方法。青い点 (ユーザーの場所) をマップの中央に配置したいのですが、ユーザーがズームインおよびズームアウトしてから、数秒後にユーザーの場所にズームインできるようにする必要もあります。

解決策に対する私の推測:ユーザーがズームインまたはズームアウトしているかどうかを検出します。あなたのヘルプは素晴らしいでしょう:)

このコードは、ユーザーの位置をズームインしますが、ズームインとズームアウトの遅延はありません。

     - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
              fromLocation:(CLLocation *)oldLocation {

  MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0); [mapView setRegion:userLocation animated:YES];


    }
4

4 に答える 4

14

ドキュメントをざっと見ると、その魔法が明らかになります。マップの を に
設定します。ここ を参照してください。userTrackingModeMKUserTrackingModeFollow


アップデート:

質問を更新したので、ここに新しい回答があります。
マップをユーザーの場所に再センタリングするには、単純なヘルパー メソッドを作成することをお勧めします。

- (void)recenterUserLocation:(BOOL)animated{
    MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);

    MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);

    [self.mapView setRegion:userRegion animated:animated];
}

ユーザーがマップの移動を停止した場合は、少し遅れて呼び出す必要があります。regionDidChangeこれは、mapView のデリゲート メソッドで行うことができます。

しかし、マップを実際にリセットする前にユーザーが地域を複数回変更した場合、reset-method をロックしないと問題が発生する可能性があります。したがって、マップを再センタリングできる場合は、フラグを立てることが賢明です。プロパティのようにBOOL canRecenter

それを初期化し、メソッドを次 のようにYES更新します。recenterUserLocation

- (void)recenterUserLocation:(BOOL)animated{
    MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);

    MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);

    [self.mapView setRegion:userRegion animated:animated];

    self.canRecenter = YES;
}

これで、ユーザーが何らかの方法でマップを移動した後、少し遅れて安全に呼び出すことができます。

- (void)mapView:(MKMapView *)mMapView regionDidChangeAnimated:(BOOL)animated{
    if (self.canRecenter){
        self.canRecenter = NO;
        [self performSelector:@selector(recenterUserLocation:) withObject:@(animated) afterDelay:3];
    }
}
于 2012-11-10T20:36:24.187 に答える
2

私も同じ問題を抱えていました。推測しました:

  1. ユーザーが地図をドラッグした場合、彼はその位置に留まりたいと考えています。
  2. ユーザーが何もしないか、現在の場所を表示するためにリセットした場合、ユーザーをフォローする必要があります。

次のように現在のユーザーの場所を表示するリセットボタンを追加しました。 ここに画像の説明を入力してください

クリックされたリセットボタンで、をに変更しneedToCenterMapましたTRUE

マップ上にドラッグジェスチャレコグナイザーを追加しました

// Map drag handler
    UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)];



- (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"Map drag ended");
        self.needToCenterMap = FALSE;
    }
}

needToCenterMap旗に応じて地図上でユーザーをフォロー

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (self.needToCenterMap == TRUE) 
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}
于 2012-12-27T09:38:52.427 に答える
1

このジョブを Map SDK に委任する方法を示す簡単な例を作成しました。もちろん、場所の変更をリッスンすることもできますが、MKUserTrackingModeFollow が自動的にこれを行うため、1 行のコードだけで済みます。

#import <MapKit/MapKit.h>

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

    //Always center the dot and zoom in to an apropriate zoom level when position changes
    [mapView setUserTrackingMode:MKUserTrackingModeFollow];

    //don't let the user drag around the the map -> just zooming enabled
    [mapView setScrollEnabled:NO];

    [self.view addSubview:mapView];
}

次に、アプリは次のようになります。 TrackingMode を使用したアプリ TrackingMode でズームアウトしたアプリ

詳細については、Apple のドキュメントを参照してください: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

于 2012-12-27T09:57:39.453 に答える
0

このシェルはトリックを行います:mkMapview.showsUserLocation = YES;

于 2012-12-27T07:53:56.537 に答える