【问题标题】:Android Google map's marker isn't centeredAndroid Google 地图标记未居中
【发布时间】:2017-04-11 08:02:19
【问题描述】:

我的布局如下:

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

目前一切正常,标记显示在地图中心。

但是当我在AutoCompleteTextView 下方添加额外的TextView(见下图,id 为edit_location_input)时,如下所示:

<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"/>

标记按预期出现在中心。

为什么某些视图会影响地图以及如何修复它?

【问题讨论】:

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


    【解决方案1】:

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

    在做

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

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2015-06-18
      • 2020-09-22
      • 2013-03-22
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多