2017-01-04 39 views
3

我希望谷歌地圖在我的應用程序總是被完全集中於用戶,並與他們作爲移動他們的當前位置的變化。 (想想口袋妖怪去,怎麼地圖實際上是與用戶移動)鎖定谷歌地圖上的用戶位置

我目前最好的實現只用動畫每次更新相機位置的位置改變,像這樣:

  // 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)); 

然而,這使得相機運動有點波動,並且落後於實際的用戶位置標記,特別是當用戶快速移動時。

有沒有一種方法,以配合谷歌地圖攝像頭的運動,因此它完全用戶的運動相匹配?

回答

3

我不認爲該標記實際上是對GoogleMap的口袋妖怪圍棋的情況下。如果你要修復在地圖中央的圖像(或任何形式的視圖)......只要確保該圖像是在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> 

所以,現在你已經在地圖的中心標誌。好的,但你仍然沒有與用戶位置同步......所以我們來解決這個問題。

我會假設你沒有問題,開始您的地圖。所以,那樣做,我會等。完成了嗎?好。地圖集。

只是不要忘記在地圖中禁用拖動:

@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] 
     } 
    } 

如果你想旋轉運動方向你的性格,你將需要隨着新的比較以前的經緯度和旋轉圖像(或視圖......或任何東西)指向移動方向。

如果您需要更多的信息,也許這正回購可以幫助你:CurrentCenterPositionMap

(回購不你想要什麼......它只用的我的解釋同一個概念。)

+0

謝謝您!工作得很好。 – Dportology