0

I am drawing a route between two markers, and I want to save that route. To do that I saved the ArrayList containing the lat and lng in the Firebase database. But I am having problems retrieving the waypoints. This is how I inserted:

  @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();

        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList();
            lineOptions = new PolylineOptions();

            List<HashMap<String, String>> path = result.get(i);

            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            lineOptions.addAll(points);
            lineOptions.width(12);
            lineOptions.color(Color.RED);
            lineOptions.geodesic(true);

            database.child("Route").child("route").setValue(points);

        }

        mMap.addPolyline(lineOptions);
    }

enter image description here

When retrieving the data, I tried to do the following:

 userRef.child("Route").child("route).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList ids = null;
            for (DataSnapshot childData : dataSnapshot.getChildren()) {
                ids.add(childData.getValue());
            }

        }
JDoe
  • 221
  • 4
  • 14

3 Answers3

1
class Route {
    private ArrayList<Location> locations;
    @PropertyName("route")
    public ArrayList<Location> getLocations() {
       return locations;
    }
    @PropertyName("route")
    public void setLocations(ArrayList<Locations> locations) {
        this.locations = locations;
    }
}


class Location{ 
    private Double latitude;
    private Double longitude;

    //getter and setter for lat - long 
}

then you can use them as

public void onDataChange(DataSnapshot dataSnapshot) {
     Route route = dataSnapshot.getValue(Route.class);
}

Now you can use the data retrieved as you want.

Edit: working code:

public class RouteActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abc);

    FirebaseDatabase.getInstance().getReference("Route").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Route route = dataSnapshot.getValue(Route.class);

            for(Location location : route.getLocations() ) {
                Log.d("stackoverflow", location.getLatitude()+","+location.getLongitude());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

public static class Route {

    private ArrayList<Location> locations;

    public Route() {

    }

    @PropertyName("route")
    public ArrayList<Location> getLocations() {
        return locations;
    }
    @PropertyName("route")
    public void setLocations(ArrayList<Location> locations) {
        this.locations = locations;
    }
}


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

    //getter and setter for lat - long

    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;
    }
}
}

This is firebase node I am querying This is firebase node I am querying

Output: D/stackoverflow: 55.39,10.33 D/stackoverflow: 65.39,13.33 D/stackoverflow: 35.39,16.33 D/stackoverflow: 85.39,12.33 D/stackoverflow: 25.39,17.33 D/stackoverflow: 57.39,61.33

Rahul
  • 4,699
  • 5
  • 26
  • 38
  • you can use this pojo to add data to firebase and it won't be such a mess of code :) – Rahul Oct 23 '17 at 14:53
  • Thanks for the answer, but this does not work. I get an error "DatabaseException: Can't convert object of type java.util.ArrayList to type "packagename" – JDoe Oct 23 '17 at 14:59
  • Add them as seperate classes as in java files and then use import – Rahul Oct 23 '17 at 16:14
  • Okay, is there a way to print out the values that are retrieved? – JDoe Oct 23 '17 at 16:54
  • you can do route.getLocations().get(0).getLatitude() to print first latitude. – Rahul Oct 23 '17 at 20:02
  • This gives me an exception "DatabaseException: Can't convert object of type java.util.ArrayList to type packagename" – JDoe Oct 24 '17 at 06:09
  • Hey, thanks again for the help yesterday. I posted a new question regarding the the same topic. If you have the time you are welcome to look at the question. https://stackoverflow.com/questions/46927477/android-cannot-retrieve-data-from-firebase – JDoe Oct 25 '17 at 09:06
  • Sorry bud.. i was busy. I hope its resolved now. If anything needed, drop me a message. – Rahul Oct 26 '17 at 09:50
0

Make this custom class with values.

class MapRouteCords{
public Double latitude;
public Double longitude;

public MapRouteCords()
{

}

 public MapRouteCords(Double latitude, Double longitude)
{
   this.latitude = latitude;
   this.longitude = longitude;
}

 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;
}

Use the method in your OnDataChange Listener

collectcords((Map<String, Object>) dataSnapshot.getValue());

Define the method.

private void collectcords(Map<String, Object> mapcords) {
    ArrayList<Double> alllats  = new ArrayList<Double>();
    ArrayList<Double> alllongs  = new ArrayList<Double>();


    for (Map.Entry<String, Object> entry : mapcords.entrySet()) {
        Map singleGame = (Map) entry.getValue();
        alllats.add((Double) singleGame.get("latitude"));
        alllongs.add((Double) singleGame.get("longitude"));
    }
Masoom Badi
  • 986
  • 9
  • 18
  • I get this exception "ClassCastException: java.util.ArrayList cannot be cast to java.util.Map" – JDoe Oct 24 '17 at 06:07
0

Here is a Nice way to get it from database : Follow these steps and at the end you will get what you want.

Step 1 : Create a class

Route

:with all the properties and getters and setters. And make sure to keep a empty constructor.

Step 2 Create an ArrayList of Route and store it in the database using

ref.setValue(arrayListRoutes);

Step 3 Getting data from Firebase Database, Assuming Datasnapshot is the snapshot on ref node. Use this onDataChange(DataSnapshot dataSnapshot):

GenericTypeIndicator<ArrayList<Route>> indicator = new GenericTypeIndicator<ArrayList<Route>>(){};

ArrayList<Route> DataList = dataSnapshot.getValue(indicator);
coder3101
  • 3,920
  • 3
  • 24
  • 28