android - 如何在数据库中保存跟踪折线?

标签 android firebase google-maps-android-api-2

我已经创建了一个函数,通过该函数我可以跟踪用户的路线并创建折线。我一直在尝试将它保存到 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/

相关文章:

android - 如何更改起点drawrect customview android

android - 如何使SlidingDrawer透明

带有 Firebase 数据字符串的 Swift Firebase UIAlertView

android - 使用 Android Google Maps Api v2 打开街道 map

android - google-maps-services 和 play-services-maps 之间的区别

Android,jfeinstein 滑动 Activity ,其中包含 Google map ,当用户触摸 map 时滑动

javascript - 在 android 的 webview 中嵌入一段 HTML 代码或 Javascript 脚本

java - Android Studio 代码样式设置不起作用

javascript - 从子进程执行node.js中的代码

python - 使用 python 的 Firestore 中的 GeoPoint