gpt4 book ai didi

Android 谷歌地图标记不居中

转载 作者:行者123 更新时间:2023-11-30 00:52:12 26 4
gpt4 key购买 nike

我的布局看起来像:

<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 并在下方使用 map 。

Activity 代码:

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

现在一切都按预期进行,标记显示在 map 的中心。

但是当我在 AutoCompleteTextView 下添加额外的 TextView(请参阅下面的 ID edit_location_input View )时,如下所示:

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

标记未显示在 map 的中心(实际上我需要缩放并移动 map 才能看到标记)。看起来它就在附近。

当我将固定宽度设置为 TextView

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

标记按预期出现在中心。

为什么有些 View 会影响 map ,如何解决?

最佳答案

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

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

map 将尝试将相机移动到您的并将其设置为缩放 6 的动画,但这两个事件可能会重叠,从而导致在不需要的 map 区域中缩放到 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();

关于Android 谷歌地图标记不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40830065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com