我已经创建了一个函数,通过该函数我可以跟踪用户的路线并创建折线。我一直在尝试将它保存到 Firebase 作为 - routeId, routeName, lat, lng
我使用的方法是,每次位置更改时,用户的新纬度和经度将保存到 Firebase 中,用户设置的 routeName 和(当前)静态 routeId。
if (mCurrentLocation != null) {
String newLat = String.valueOf(mCurrentLocation.getLatitude());
String newLng = String.valueOf(mCurrentLocation.getLongitude());
LatLng newPoint = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
List<LatLng> points = route.getPoints();
points.add(newPoint);
route.setPoints(points);
Map<String, String> routePoint = new HashMap<String,String>();
routePoint.put("routeID", "124");
routePoint.put("routeName", saveRouteTitle);
routePoint.put("lat", newLat);
routePoint.put("lng", newLng);
mFirebaseRef.push().setValue(routePoint);
}
但是,当我像这样使用适配器检索数据时,
@Override
protected void populateView(View view, Route route) {
// Map a Chat object to an entry in our listview
String routeName = route.getRouteName();
int routeID = route.getRouteID();
String lat = route.getLat();
String lng = route.getLng();
TextView tvRouteName = (TextView) view.findViewById(R.id.tvRouteName);
tvRouteName.setText(routeName + ": ");
}
我得到的是“route”的每个子项的 ListView ,这意味着如果我设置的 routeName 是“Walking”
,将会有一个列表充满“Walking”。
我想将同一 routeName 的所有 lat 和 longs 保存在一起,然后在 ListView 中检索它们,这样当用户选择 routeName 时,我可以使用数据库中的 lat 和 lngs 在 map 上重新生成折线。这可以做到吗?我不介意不使用 Firebase,但在线服务器更适合备份。
最佳答案
Firebase 是存储和检索此类数据的理想选择。
例如,假设您让用户输入路线名称和 ID,并且您想要存储从起点到目的地的沿途路径(每个航点的纬度和经度):
可以将数据存储在 Firebase 中的一种方法是:
random_route_node_name
routeName: "a route name"
routeId: "some route id"
waypoints
random_waypoint_node_name
lat: "latitude"
lon: "longitude"
random_waypoint_node_name
lat: "latitude"
lon: "longitude"
random_route_node_name
routeName: "a route name"
routeId: "some route id"
waypoints
random_waypoint_node_name
lat: "latitude"
lon: "longitude"
random_waypoint_node_name
lat: "latitude"
lon: "longitude"
在每个父节点 (random_route_node_name) 中,您有 routeName、id,然后是路径上的路标,每个路标都有经度和纬度。
需要指出几件重要的事情。
random_route_node_name 和 random_waypoint_node_name 将是使用 Firebase childByAutoId 或其他一些非特定名称生成的名称。这是因为节点的名称不应直接绑定(bind)到数据,因此可以在不影响节点名称的情况下更改数据。航路点也是如此;可以在不影响子父名称的情况下修改航点子数据
我不知道是否需要查询航点的功能,但如果需要,应该做的另一件事是展平这些数据——每条路线的航点可以存储在一个单独的节点中,这样可以进行查询对于高级别的特定航路点。例如,用户想要了解其当前位置半径范围内的所有航路点。
实际上,查询只能在特定路线内进行。
关于android - 如何在数据库中保存跟踪折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892881/