android - 如何使 GEO Point Class Parcelable 或 Serializable 因为我想使用 Intent 传递它们
问题描述
我使用 Cloud Firestore 作为我的数据库,并且在每个文档中都有一个名为 restaurant_info 的地图,它通常存储 <"name","Name of the Restaurant">、<"Location",GEOPoint of the Restaurant> 等等字段。问题是 Geo Point 类没有实现 Parcelable 或 Serializable 接口。
解决方案
有两种方法可以解决这个问题。第一个是添加到Restaurant
实现Parcelable
接口的类中:
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
and存储long
在你的Restaurant
类中作为双原语,每次你需要一个 GeoPoint 对象时,像这样创建它:
GeoPoint geoPoint = new GeoPoint(restaurant.getLatitude(), restaurant.getLongitudeE6());
推荐阅读
- javascript - 我需要帮助才能在 Xcode 中创建的简单 webview 应用程序中运行 Javascript Alert & Confirm
- python - Python中的Readlines方法在标题后跳过文件中的第一行
- docker-compose - 泊坞窗中的 Corda
- javascript - 如何在jQuery中的两个字符串之间获取url的特定部分
- docker - 获取 docker-compose 创建的容器的 ID
- java - 在java中按不同的值对BufferedWriter字符串输出进行排序
- java - 如何在 javafx 中的 tableView 的特定行中接收数据?
- android - android 4.4.2无法使用文件解决方案2019上传文件?
- pycharm - 龟函数在pycharm中不起作用
- java - Azure Java SDK - 同时使用映像创建多个 VM