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

How to make GEO Point Class Parcelable or Serializable as I want to pass them using Intent

我使用 Cloud Firestore 作为我的数据库,在每个文档中都有一个名为 restaurant_info 的地图,它通常存储 <"name"、"Name of The Restaurant">、<"Location", 餐厅的 GEOPoint> 和更多字段。问题是 Geo Point class 没有实现 Parcelable 或 Serializable 接口。

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

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 class 中,每次你需要一个 GeoPoint 对象时,创建它就像这个:

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