2016-11-27 23 views
0

我的佈局是這樣的:的Android谷歌地圖的標記不居中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar"> 
    </include> 

    <AutoCompleteTextView 
     android:id="@+id/edit_location_input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_location_input_hint"/> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

所以我在屏幕的上方和下方的地圖使用AutoCompleteTextView

活動代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_edit_location); 
    ... 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    mMap.getUiSettings().setZoomControlsEnabled(true); 

    LatLng point = new LatLng(mService.getLocation().getLatitude(), mService.getLocation().getLongitude()); 
    mMap.addMarker(new MarkerOptions().position(point).title(mService.getName())); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(point)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); 
} 

現在一切正常,並在地圖的中心顯示的標記。

但是當我添加額外的TextView(見下圖與ID edit_location_input)以下AutoCompleteTextView這樣的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar"> 
    </include> 

    <AutoCompleteTextView 
     android:id="@+id/edit_location_input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_location_input_hint"/> 

    <TextView 
     android:id="@+id/edit_location_result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

標記不是在地圖上(其實我需要的中心所示放大並移動地圖以查看標記)。看起來像它位於附近。

當我設置固定寬度與TextView

<TextView 
     android:id="@+id/edit_location_result" 
     android:layout_width="match_parent" 
     android:layout_height="40dp"/> 

標記如預期出現在中心。

爲什麼有些視圖會影響地圖以及如何解決?

回答

0

問題不在於你的佈局,而在於你正在做的中心標記的相機動作/動畫。

mMap.moveCamera(CameraUpdateFactory.newLatLng(point)); 
mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); 

地圖會嘗試將攝像機移動到您的point和動畫它來放大6個,但在這兩個事件可以重疊導致不期望的地圖區域縮放到16級。

爲了解決最簡單的解決方案是更新相機只有一個運動問題:

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 16)); 

如果您還需要移動相機,然後動畫它,你可以從MapUtils使用CameraUpdateAnimator (我的一個項目):

CameraUpdateAnimator animator = new CameraUpdateAnimator(mMap, null); 

animator.add(CameraUpdateFactory.newLatLng(point), false, 100); 
animator.add(CameraUpdateFactory.zoomTo(16), true, 1000); 

animator.execute();