如何在数据库中保存跟踪多段线? [英] How to save tracking polylines in database?

查看:147
本文介绍了如何在数据库中保存跟踪多段线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,可以跟踪用户的路线并创建一条折线。我一直在尝试将其保存到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);

地图< 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);

$ / code>

但是,当我使用适配器检索数据时, p>

  @Override 
保护void populateView(视图视图,路由路径){
//将聊天对象映射到在我们的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 +:);

$ / code $ / pre

我得到的是每一个路由的孩子的列表视图,这意味着如果我设置的routeName是Walking,将会出现一个名为Walking的列表。
我想一起保存所有相同的路由名称的经纬度和长度,然后在列表视图中检索它们,所以当用户选择路由名称时,我可以使用数据库中的lat和lngs重新生成地图上的折线。可以这样做吗?我不介意不使用Firebase,但是在线服务器对于备份更为理想。

解决方案

存储和检索这种类型的数据。

例如,假设你有用户输入一个路由名称和id,并且你想沿路径存储路点(纬度和长度对于每个路点)从开始到目的地:

可以在Firebase中存储数据的一种方法是:

 random_route_node_name 
routeName:一个路由名称
routeId:一些路由ID
航点
random_waypoint_node_name
lat:纬度
lon:longitude
random_waypoint_node_name
lat:latitude
lon:longitude
random_route_node_name
routeName:路由名称
routeId:some route id
航点
random_waypoint_node_name
lat:纬度
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或其他非特定名称生成的名称。这是因为节点的名称不应直接与数据绑定,因此可以更改数据而不影响节点名称。路标也是一样的;航点子数据可以在不影响子父级名称的情况下进行修改

我不知道是否需要功能来查询航点,但如果是这样的话,要做的就是把这些数据弄平 - 每条路线的航点可以存储在一个单独的节点中,这样可以在高层查询特定的航点。例如,用户想要知道在他们当前位置半径内的所有路标。

实际上,查询只能在特定的路线中完成。

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.

解决方案

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.

这篇关于如何在数据库中保存跟踪多段线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆