我使用 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);
}
第二种方法是将 lat
和 long
作为双基元存储在 Restaurant
类中,每次您需要一个 GeoPoint 对象时,像这样创建它:
GeoPoint geoPoint = new GeoPoint(restaurant.getLatitude(), restaurant.getLongitudeE6());
关于android - 如何使 GEO Point Class Parcelable 或 Serializable 因为我想使用 Intent 传递它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482035/