我正在使用 SupportMapFragment 在 ScrollView 中显示静态 map 。我不喜欢移动/缩放 map ,只是显示位置。
当我向下/向上滚动时, map 会在其范围内晃动,感觉很慢。我的问题是,如何才能消除这种滞后,或者如何使用 v2 api 映射的静态版本。
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_half"
android:background="@color/white"
android:orientation="vertical" >
...
<fragment
android:id="@+id/fragmentMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/viewDivider"
android:layout_margin="@dimen/margin_half"
/>
<View
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignBottom="@id/fragmentMap"
android:layout_alignLeft="@id/fragmentMap"
android:layout_alignRight="@id/fragmentMap"
android:layout_alignTop="@id/fragmentMap"
android:background="@android:color/transparent" />
...
</RelativeLayout>
这与MapFragment in ScrollView 没有真正的关系,尽管我使用这个技巧去除了旧设备上的黑色剪裁,正如您在透明 View 中看到的那样。
我还禁用了所有手势和 map View 的点击功能:
getMap().getUiSettings().setAllGesturesEnabled(false);
getMap().getUiSettings().setZoomControlsEnabled(false);
getMap().getUiSettings().setMyLocationButtonEnabled(false);
...
mapView.setClickable(false);
mapView.setFocusable(false);
mapView.setDuplicateParentStateEnabled(false);
最佳答案
随着新版 Google Play 服务的发布,Google 添加了一个 snapshot方法以检索当前显示的 map 的位图,该 map 可能会显示。所以交互是不可能的,但至少 map 不再摇晃了。
使用
GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)
并实现 SnapshotReadyCallback。交付快照后,将 MapFragment 替换为包含快照的 ImageView。
此示例在 onResume 方法中运行良好:
@Override
public void onResume() {
super.onResume();
notifyDataChanged();
final ViewTreeObserver viewTreeObserver = fragmentMap.getView()
.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
onMesuredMap(this);
}
});
}
}
private void onMesuredMap(OnGlobalLayoutListener listener) {
if (fragmentMap.getView().getWidth() != 0
&& fragmentMap.getView().getHeight() != 0) {
fragmentMap.getView().getViewTreeObserver()
.removeOnGlobalLayoutListener(listener);
makeSnapShot();
}
}
private void makeSnapShot() {
Runnable action = new Runnable() {
@Override
public void run() {
fragmentMap.getMap().snapshot(new SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap arg0) {
imageViewStaticMap.setImageBitmap(arg0);
removeMapFragment();
}
});
}
};
//litle hack to make sure that the map is loaded for sure
fragmentMap.getView().postDelayed(action, 1500);
}
private void removeMapFragment() {
if (fragmentMap.isVisible()) {
getChildFragmentManager()
.beginTransaction()
.remove(fragmentMap)
.commit();
}
}
旁注: 要使用新版本,请运行 Android SDK 管理器的更新,并在使用 maven 时运行 maven-android-sdk-deployer与:
mvn install -fae
关于在 Scrollview 中滚动时 Android v2 MapFragment 抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17829715/