Reputation: 221
The concept of my application is that a a path is drawn every time a user sets two markers on a map and that path is shown in another activity. I have saved the path (waypoints) as an Array list in my FireBase database and I have also retrieved the way points in the second activity, but I am having problems displaying the poly line in the second activity. My database:
My POJO classes:
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;
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;
}
}
Retrieval of waypoints:
userRef.child("Route").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
for (Location location : route.getLocations()) {
points = new ArrayList();
double lat = location.getLatitude();
double lng = location.getLongitude();
position = new LatLng(lat, lng);
points.add(position);
}
}
To add the polyline to the map I do this:
PolylineOptions lineOptions = null;
lineOptions.add(position);
lineOptions.width(12);
lineOptions.color(Color.RED);
mMap.addPolyline(lineOptions);
But I get this exception :
"NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.PolylineOptions com.google.android.gms.maps.model.PolylineOptions.add(com.google.android.gms.maps.model.LatLng)' on a null object reference"
Upvotes: 2
Views: 850
Reputation: 16379
What about writing
PolylineOptions lineOptions = new PolylineOptions();
instead of
PolylineOptions lineOptions = null;
Also you are adding a single point in the polyline. Loop through your list points
and add all of them.
for (LatLng point: points){
lineOptions.add(point);
}
mMap.addPolyline(lineOptions);
Your another mistake:
You are recreating the ArrayList as points = new ArrayList();
inside for loop. Use it before for loop.
points = new ArrayList();
for (Location location : route.getLocations()) {
//your code
}
Upvotes: 3