2016-11-27 4 views
0

Моя раскладка выглядит следующим образом:Android Google Map является не центром

<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 (см ниже точки зрения с идентификатором 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)); 

Если вам все еще нужно, чтобы переместить камеру, а затем оживить его, вы можете использовать CameraUpdateAnimator из MapUtils (проект шахты):

CameraUpdateAnimator animator = new CameraUpdateAnimator(mMap, null); 

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

animator.execute();