Android - 显示多段线 [英] Android - Display Polylines

查看:139
本文介绍了Android - 显示多段线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动的应用程序。在第一个活动中,用户可以在地图上设置两个标记,并在两个标记之间绘制路径。在第二个活动中,我想显示在第一个活动中绘制的所有路径。为此,我保存Firebase实时数据库中路径的路标,并在第二个活动中检索数据。



截至目前,第二个活动仅显示一条路径从数据库。我想要做的是在第二个活动中显示用户在第一个活动(保存在数据库中)中制作的所有路径。我假设我必须做一些循环来做到这一点。任何想法如何做到这一点?



POJO类 - 路线:

  public class Route {

private ArrayList< Location>位置;

public Route(){
}

public ArrayList< Location> getLocations(){
返回位置;


public void setLocations(ArrayList< Location> locations){
this.locations = locations;


$ / code $ / pre

POJO类 - 位置:

  public static class Location {
private Double latitude;
私人双倍经度;
$ b public Location(){
}

public double getLatitude(){
return latitude;
}

public void setLatitude(Double latitude){
this.latitude = latitude;
}

public Double getLongitude(){
return longitude;
}

public void setLongitude(Double longitude){
this.longitude = longitude;


$ / code $ / pre
$ b $ p我的数据库



我如何检索数据并添加多段线来映射:

  userRef.child(sharedPreferences.getString(school,null))。child(routes)。child(sh.getString(key ,null))。addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
Route route = dataSnapshot.getValue(Route.class);
for(Location location:route.getLocations()){
double lat = location.getLatitude();
double lng = location.getLongitude();
position = new LatLng(lat,lng );
points.add(position);
}
}


PolylineOpt离子lineOptions =新的PolylineOptions(); (LatLng point:points)

lineOptions.add(point);
}
lineOptions.width(12);
lineOptions.clickable(true);
lineOptions.color(Color.RED);
mMap.addPolyline(lineOptions);


解决方案

在当前的例子中,到 $ school / routes 下的单个节点,所以它只会检索那个节点。



如果你需要一次检索所有的路由,你需要附加一个监听器到 routes 节点,并且逐个遍历这些子节点。有关处理数据列表的文档
$ b


将ValueEventListener附加到数据列表中会将整个数据列表作为单个DataSnapshot返回,然后您可以循环访问个人的孩子。

类似这样的工作应该可以完成这项工作:

<$ p $ ();} $ {code} public void drawAllRoutes(){
userRef.child(sharedPreferences.getString(school,null))。child(routes)。addListenerForSingleValueEvent(new ValueEventListener(){$ b (DataSnapshot child:dataSnapshot.getChildren()){
路由路径= child.getValue(Route.class); $ b $覆盖$ b $公共无效onDataChange(DataSnapshot dataSnapshot){
$ b drawRoute(route);
}
}

@Override
public void onCancelled(DatabaseError databaseError){}
});


public void drawRoute(Route route){
PolylineOptions lineOptions = new PolylineOptions();

(位置位置:route.getLocations()){
double lat = location.getLatitude();
double lng = location.getLongitude();
lineOptions.add(new LatLng(lat,lng));
}

lineOptions.width(12);
lineOptions.clickable(true);
lineOptions.color(Color.RED);

mMap.addPolyline(lineOptions);



drawAllRoutes()方法将侦听器附加到 $ school / routes 节点上,它将为您提供列表的 DataSnapshot 在该节点下,在 DataSnapshot#getChildren()中提供每个单独的子节点。你可以迭代这些子元素来分别绘制每条折线。



我也将路线绘制逻辑移动到 drawRoute() code>方法,这样它就可以在地图上绘制 Route 的任何实例,而不管这个 Route >
$ b

drawRoute()方法使用 mMap 变量 - 这就是你的 GoogleMap 实例 - 所以你只能调用 drawAllRoutes()方法:

  private GoogleMap mMap; 

// ...

@Override
public void onMapReady(final GoogleMap googleMap){
mMap = googleMap;

// ...

drawAllRoutes();
}


I have an application with two activities. In the first activity the user is able to set two markers on the map and a path will be drawn between the two markers. In the second activity I want to display all of the paths drawn in the first activity. To do this, I save the waypoints of the paths in a Firebase realtime database and retrieve the data in the second activity.

As of now the second activity only displays one path from the database. What I want to do is to display all paths that the user made in the first activity (saved in the database) in the second activity. I am assuming I have to do some kind of loop to do this. Any ideas on how to do this?

POJO class - Route:

public class Route {

    private ArrayList<Location> locations;

    public Route() {
    }

    public ArrayList<Location> getLocations() {
        return locations;
    }

    public void setLocations(ArrayList<Location> locations) {
        this.locations = locations;
    }
}

POJO class - Location:

public static class Location {
    private Double latitude;
    private Double longitude;

    public Location() {
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
}

My Database:

How I retrieve data and add polyline to map:

  userRef.child(sharedPreferences.getString("school", null)).child("routes").child(sh.getString("key", null)).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Route route = dataSnapshot.getValue(Route.class);
            for (Location location : route.getLocations()) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                position = new LatLng(lat, lng);
                points.add(position);
            }
        }


 PolylineOptions lineOptions = new PolylineOptions();
    for (LatLng point : points) {
        lineOptions.add(point);
    }
    lineOptions.width(12);
    lineOptions.clickable(true);
    lineOptions.color(Color.RED);
    mMap.addPolyline(lineOptions);

解决方案

In your current example, you're attaching a listener to a single node under $school/routes, so it will only retrieve that node.

If you need to retrieve all routes at once, you'll need to attach a listener to the routes node and iterate through the children individually. From the documentation on working with lists of data:

Attaching a ValueEventListener to a list of data will return the entire list of data as a single DataSnapshot, which you can then loop over to access individual children.

Something like this should do the job:

public void drawAllRoutes() {
    userRef.child(sharedPreferences.getString("school", null)).child("routes").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Route route = child.getValue(Route.class);
                drawRoute(route);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}

public void drawRoute(Route route) {
    PolylineOptions lineOptions = new PolylineOptions();

    for (Location location : route.getLocations()) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        lineOptions.add(new LatLng(lat, lng));
    }

    lineOptions.width(12);
    lineOptions.clickable(true);
    lineOptions.color(Color.RED);

    mMap.addPolyline(lineOptions);
}

The drawAllRoutes() method attaches a listener to the $school/routes node instead, which will provide you with a DataSnapshot of the list under that node, with each individual child node provided in DataSnapshot#getChildren(). You can then iterate over these children to draw each polyline separately.

I have also moved your route-drawing logic into a drawRoute() method, so that it can draw any instance of Route on the map, regardless of how this Route object was created.

The drawRoute() method makes use of the mMap variable - that is your GoogleMap instance from the view - so you can only call the drawAllRoutes() method once the map is ready:

private GoogleMap mMap;

// ...

@Override
public void onMapReady(final GoogleMap googleMap) {
    mMap = googleMap;

    // ...

    drawAllRoutes();
}

这篇关于Android - 显示多段线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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