我正在使用Cloud Firestore作为我的数据库,在每个文档中都有一个名为restaurant_info的Map,其中包含<"name","餐厅名称">、<"Location",餐厅的GEOPoint>和其他一些字段。问题在于Geo Point类没有实现Parcelable或Serializable接口。
我正在使用Cloud Firestore作为我的数据库,在每个文档中都有一个名为restaurant_info的Map,其中包含<"name","餐厅名称">、<"Location",餐厅的GEOPoint>和其他一些字段。问题在于Geo Point类没有实现Parcelable或Serializable接口。
有两种方法可以解决这个问题。第一种方法是在实现了Parcelable
接口的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);
}
lat
和long
作为double基元类型存储在您的Restaurant
类中,每次需要GeoPoint对象时,像这样创建它:GeoPoint geoPoint = new GeoPoint(restaurant.getLatitude(), restaurant.getLongitudeE6());