Android - 显示多段线 [英] Android - Display Polylines
问题描述
截至目前,第二个活动仅显示一条路径从数据库。我想要做的是在第二个活动中显示用户在第一个活动(保存在数据库中)中制作的所有路径。我假设我必须做一些循环来做到这一点。任何想法如何做到这一点?
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()$ c $一旦地图准备就绪,就可以使用c>方法:
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屋!