Android Google地图的标记并非居中

时间:2016-11-27 14:30:52

标签: android android-layout google-maps-android-api-2

我的布局如下:

<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(请参见下面的ID为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"/>

标记按预期显示在中心。

为什么有些观点会影响地图以及如何解决?

1 个答案:

答案 0 :(得分:1)

问题不在于您的布局,而在于您要对标记进行居中的相机移动/动画。

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