2017-01-04 6 views
3

自分のアプリケーションのGoogleマップを常にユーザーの中心に置いて、現在の場所の変更に合わせて移動します。ユーザーのGoogleマップをロックする場所

  // update the location of the camera based on the new latlng, but keep the zoom, tilt and bearing the same 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 
        googleMap.getCameraPosition().zoom, MAX_TILT, googleMap.getCameraPosition().bearing)); 
     googleMap.animateCamera(cameraUpdate); 

     googleMap.setLatLngBoundsForCameraTarget(toBounds(latLng, 300)); 

これが行いますので、は次のように私の現在の最良の実装は単に、アニメーションとカメラの位置に場所が変更されるたびに更新

(マップが実際にユーザーにどのように移動するか、ポケモンが行く考えます)カメラの動きはやや不安定になり、特にユーザーがすばやく動いている場合は、実際のユーザーの位置マーカーより遅れます。

Googleマップカメラの動きをつなぎ合わせて、ユーザーの動きに正確に一致する方法はありますか?

答えて

3

ポケモンゴーの場合、マーカーが実際にGoogleマップにあるとは思われません。地図の中心に画像(またはあらゆる種類の画像)を修正したい場合は...画像がXMLファイルの地図の中央にあるようにしてください。このよう

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:src="@mipmap/ic_self_position"/> 

     <fragment 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.SupportMapFragment"/> 

    </RelativeLayout> 

は、だから今は、マップの中央にマーカーを持っています。しかし、あなたはまだそれをユーザーの位置と同期させていません...だから、これを解決しましょう。

マップを開始する際に問題がないと仮定します。それで、私は待っています。終わった? OK。マップセット。

はちょうどあなたのマップにドラッグを無効にすることを忘れないでください:

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     googleMap.getUiSettings().setScrollGesturesEnabled(false); 

     ... 
} 

はのは、ユーザーの場所を受け取り、マップを移動してみましょう。

https://developer.android.com/training/location/receive-location-updates.html

しかし、これまでのコードの一部を変更します:

public class MainActivity extends ActionBarActivity implements 
      ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 
     ... 
     @Override 
     public void onLocationChanged(Location location) { 
      mCurrentLocation = location; 
      mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
      moveUser(Location location); 
     } 

     private void moveUser(Location location) { 
      LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
      mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

    mCharacterImageView.animate(pretendThatYouAreMovingAnimation) 
[you can make a animation in the image of the user... like turn left/right of make walk movements] 
     } 
    } 

あなたが移動方向に自分のキャラクターを回転させたい場合は、あなたがする必要があります。このリンクを使用して

以前のLatlngを新しいLatlngと比較し、画像(またはビューなど)を回転させて、移動方向を指すようにします。

あなたはより多くの情報が必要な場合は、多分このレポはあなたを助けることができる:CurrentCenterPositionMap

(レポはあなたがやりたいことはありません...それだけで私の説明の同じ概念を使用しています。)

+0

感謝君は!非常にうまくいった。 – Dportology

関連する問題