gpt4 book ai didi

android - GoogleMap 本身是可打包的吗?

转载 作者:行者123 更新时间:2023-11-29 01:03:55 26 4
gpt4 key购买 nike

我是 java 和 android 的新手。我遇到了一个基本问题。

在这个给定的 fragment 中,我可以将 GoogleMap 对象添加为 parcelable 而无需任何额外的 pracelable 类:

    public class SecondFragment extends Fragment
implements OnMapReadyCallback {

public GoogleMap mMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (mMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}

这是我第一次遇到这种可分割的方法,所以,我在另一个类中尝试了同样的字符串:

public String location;//="No location name found";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
location = savedInstanceState.getParcelable(KEY_LOCATION_NAME);
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
if (location != null) {
outState.putParcelable(KEY_LOCATION_NAME, location);
super.onSaveInstanceState(outState);
}
}

这显示编译时错误:

Error:(205, 49) error: incompatible types: String cannot be converted to Parcelable

那么,我的问题是为什么同样的事情适用于 googleMap 而不是字符串? googleMap 是否天生可打包,对于“普通”字符串,我们必须创建一个可打包类吗?

最佳答案

您可以阅读 GoogleMap类页面以查看它 实现 Parcleable

the same thing is working for googleMap

您没有保存 map ...您正在保存 CameraPosition

location 是一个字符串,而不是 Google map 。

如果你想保存位置,一个LatLng位置 Parcelable

for "normal" String, we must create a parcelable class?

没有。对于字符串,只需使用 outState.putString()

关于android - GoogleMap 本身是可打包的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002318/

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