How to save tracking polylines in database?

2019-02-20 01:25发布

问题:

I have created a function whereby I am able to track the user's route and create a polyline. I have been trying to save it to Firebase as - routeId, routeName, lat, lng

The method I have used is, everytime on a location change, the user's new lat and long will be saved into Firebase, a routeName that the user has set and (currently) a static 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);
    }

However, when I retrieve the data using an adapter, like so,

@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 + ": ");
}

what I get is a listview of each child of "route", which means if the routeName I had set was "Walking", there will be a list filled with "Walking". I want to save all the lat and longs of the same routeName together, and then retrieve them in a listview so when the user selects the routeName I can regenerate the polyline on the map with the lat and lngs from the database. Could this be done? I don't mind not using Firebase, but an online server would be more optimal for backup.

回答1:

Firebase would be ideal for storing and retrieving this type of data.

For example, suppose you have the user enter a route name and id and you want to store waypoints along the path (lat and long for each waypoint) from start to destination:

One way the data could be stored in Firebase is:

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"

Within each parent node (random_route_node_name) you have the routeName, the id and then the waypoints along the path, each waypoint has a longitude and latitude.

A couple of important things to point out.

The random_route_node_name and random_waypoint_node_name would be names generated with Firebase childByAutoId or some other non-specific name. This is because the name of the node should not be tied directly to the data so the data can be changed without affecting the node name. The same thing goes for the waypoints; the waypoint child data can be modified without affecting the child parent name

I don't know if functionality is needed to query for waypoints, but if so, other thing that should be done is to flatten this data - the waypoints for each route could be stored in a separate node which would allow for queries for specific waypoints at a high level. For example, a user wants to know all of the waypoints within a radius of their current location.

As it is, queries could only be done within a specific route.