作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的布局看起来像:
<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/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!