android - 如何使 GEO Point Class Parcelable 或 Serializable 因为我想使用 Intent 传递它们

标签 android firebase google-cloud-firestore parcelable

我使用 Cloud Firestore 作为我的数据库,在每个文档中都有一个名为 restaurant_info 的 map ,它通常存储 <"name","Name of The Restaurant">, <"Location",GEOPoint of the Restaurant>还有一些领域。问题是 Geo Point 类没有实现 Parcelable 或 Serializable 接口(interface)。

最佳答案

有两种方法可以解决这个问题。第一个是添加到实现 Parcelable 接口(interface)的 Restaurant 类:

class Restaurant implements Parcelable {}

然后覆盖 writeToParcel() 方法并创建另一个构造函数,如下所示:

private GeoPoint geoPoint;

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeDouble(geoPoint.getLatitude());
    parcel.writeDouble(geoPoint.getLongitude());
}

public Restaurant(Parcel in) {
    Double lat = in.readDouble();
    Double lng = in.readDouble();
    geoPoint = new GeoPoint(lat, lng);
}

第二种方法是将 latlong 作为双基元存储在 Restaurant 类中,每次您需要一个 GeoPoint 对象时,像这样创建它:

GeoPoint geoPoint = new GeoPoint(restaurant.getLatitude(), restaurant.getLongitudeE6());

关于android - 如何使 GEO Point Class Parcelable 或 Serializable 因为我想使用 Intent 传递它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482035/

相关文章:

javascript - Firebase 实时数据库字段查询

firebase - 使用 Firebase + Vue 构建 Web 应用程序时,我们真的需要 Vuex 吗?

android - 当我处于离线状态时,Firestore 添加方法无法正常工作

reactjs - 取消 useEffect Hook 中的所有异步/等待任务以防止 react 中内存泄漏的正确方法是什么?

java - 与Android中的equals()方法比较

android - 在 RecyclerView Adapter 中使用带有 Activity 作为参数的方法

android - 在两个应用程序之间共享字符串

android - 语音识别API,Google Voice会做这个吗?

android - 使用 firebase 重置密码

firebase - 检查 Field 是否已经存在于 Flutter Firestore 中