2011-02-17 14 views
6

Googleマップと同じようにユーザーの青い点に従いたいと思います。それは、場所が変化しているときに地図(またはその中心)がスムーズにそれに従うことを意味します。しかし、私は、デリゲートで標準的な方法を使用する場合:mapview.userLocationMKMapViewのユーザーの位置をスムーズにフォロー

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    [map setCenterCoordinate:map.userLocation.coordinate animated:YES]; 
} 

またはキー値オブザーバ、マップの移動は、それはすぐに私がanimatedパラメータを使用する場合でも、新しい場所に「ジャンプ」はかなり「途切れ」です。

さらに、ブルードット自体はネイティブのGoogleマップアプリのようにスムーズに動かず、そこで移動する代わりに新しい場所にジャンプすることが多いと思います。

ありがとうございました。

+0

iOS 5では、この機能にトラッキングモードが追加されています。最後に! – d0n13

答えて

3

「ハイテクのJak

私もこの問題を解決しようとしたが、MKMapViewは、次の位置を予測し、動きがより滑らかにするために、更新の間でいくつかの空想のものを使用している場合がありますように見えます。

コンパスを扱う方法を見ると、元のコンパスのデータはひどく安定していないため、フィルタリングが大量に行われているため、実際に滑らかで滑らかなものになります。

また、ズームすることはできますが、パンすることはできません。私はスクロールをオフにしても、私のアプリでこの機能を再現することに失敗しました。

私は、彼らはちょうど我々の前に保つために、すべてを公開していないと思います:)あなたも、この試みることができる D

+0

まあ、私は同じ結論に達しました。 ; o)マップアプリは、私たちができないいくつかのプライベートメソッドを使用しなければなりません。私は 'setCenterCoordinate:animated:'をUIViewアニメーションに入れようとしましたが効果はありません。 – JakubM

0

- (void)adjustRegion:(CLLocation*)location { 
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 150.0, 150.0); 
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion]; 
    [self.mapView setRegion:adjustedRegion animated:YES]; 
} 
+0

'CMModel'とは何ですか? – Raptor

+1

@ShivanRaptorああ、それは私がいくつかのパラメータを保存したシングルトンクラスです。その行をコピーして貼り付けただけです。 Sry、私は私の答えを編集します。 – borisdiakur

+0

@Legoは、ユーザーが定義したズームレベルを使用するよう強制していませんか?(カスタムで定義するのではなく) – RonLugge

1

は、私が現在を取得するためにCLLocationManagerデリゲート更新メソッドを使用前の位置を保存し、それらの間に補間した。もう少し努力しますが、トラッキングをよりスムーズにします。

#define zoomSpan MKCoordinateSpanMake(0.001f, 0.001f) 

@property (strong, nonatomic) CLLocation *lastLocation; 
@property (strong, nonatomic) MKMapView *mapView; 
@property (strong, nonatomic) CLLocationManager *locationManager; 

... 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *currentLocation = locations[locations.count - 1]; 
    // Set to last location, for faster setting to correct position 
    [self.mapView setRegion:MKCoordinateRegionMake(self.lastLocation.coordinate, zoomSpan) animated:NO]; 

    // Locations in-between coordinates for smooth tracking 
    CLLocationDegrees deltaLat = currentLocation.coordinate.latitude - self.lastLocation.coordinate.latitude; 
    CLLocationDegrees deltaLon = currentLocation.coordinate.longitude - self.lastLocation.coordinate.longitude; 
    NSArray *locationsArray = @[[[CLLocation alloc] initWithLatitude:(self.lastLocation.coordinate.latitude + deltaLat * 0.2) 
                  longitude:(self.lastLocation.coordinate.longitude + deltaLon * 0.2)], 
           [[CLLocation alloc] initWithLatitude:(self.lastLocation.coordinate.latitude + deltaLat * 0.4) 
                  longitude:(self.lastLocation.coordinate.longitude + deltaLon * 0.4)], 
           [[CLLocation alloc] initWithLatitude:(self.lastLocation.coordinate.latitude + deltaLat * 0.6) 
                  longitude:(self.lastLocation.coordinate.longitude + deltaLon * 0.6)], 
           [[CLLocation alloc] initWithLatitude:(self.lastLocation.coordinate.latitude + deltaLat * 0.8) 
                  longitude:(self.lastLocation.coordinate.longitude + deltaLon * 0.8)], 
           currentLocation]; 

    float timer = 0; 
    for (CLLocation *nextLocation in locationsArray) { 
     [self performSelector:@selector(updateMapCenterLocation:) withObject:nextLocation afterDelay:timer]; 
     timer += 0.2; 
    } 
    self.lastLocation = currentLocation; 
} 

- (void)updateMapCenterLocation:(CLLocation *)location 
{ 
    [self.mapView setRegion:MKCoordinateRegionMake(location.coordinate, zoomSpan) animated:YES]; 
} 

は、あなたが(マップビューを保持し、通常はビューコントローラ)でこれを使用しているクラスはCLLocationManagerのデリゲートとCLLocationManagerDelegateプロトコルを実装に設定されていることを確認してください:ここに私のコードです。

いくつかの追加コメント:

  • このコードの動作はMKUserTrackingに似ていますが、これは、カスタム設定したズームレベルをサポートしていません。トラッキングを開始するときにズームレベルをリセットするバグがあるように思われます。この回避策です。
  • zoomSpanは現在固定されていますが、現在の地域のスパンをself.mapView.region.spanとすることもできます。
関連する問題