David
David

Reputation: 1770

google maps v2 draw polyline depends on speed

i have this database: enter image description here

and i've tried to make a simple polyline drawing which is depends on the speed this way:

Cursor curTAB = myDataBase.rawQuery("SELECT * FROM  GPS_Values;", null);    
Integer count = 0;
while (curTAB.moveToNext()) {
String s_latitude = curTAB.getString(1);
String s_longitude = curTAB.getString(2);
String s_speed = curTAB.getString(5);    
count++;    
double latitude = Double.parseDouble(s_latitude);
double longitude = Double.parseDouble(s_longitude);
int speed = Integer.parseInt(curTAB.getString(5).toString());
if (speed <= 50) {
Log.i("Coordinates",
 s_latitude.toString() + " --- " + s_longitude.toString() + " --- " +s_speed.toString());
line.add(new LatLng(latitude, longitude)).color(Color.GREEN);
line.color(Color.GREEN);
map.addPolyline(line);
} else {
Log.e("Coordinates",
s_latitude.toString() + " --- " + s_longitude.toString() + " --- " +s_speed.toString());
line.add(new LatLng(latitude, longitude)).color(Color.BLUE);
line.color(Color.BLUE);
map.addPolyline(line);
}
Log.i("Coordinates",
s_latitude.toString() + " --- " + s_longitude.toString() + " --- " +s_speed.toString());
}
curTAB.close();
myDataBase.close();  
double latitude = Double.parseDouble(first_lat);
double longitude = Double.parseDouble(first_long);map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
longitude), 15));
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

But every line is with the same color. Please help me what did i wrong!

update: the working code(at the last point it doesn't work but not a big problem)

Integer count = 0;
        while (curTAB.moveToNext()) {
            String s_latitude = curTAB.getString(1);
            String s_longitude = curTAB.getString(2);
            String s_speed = curTAB.getString(5);

            double latitude = Double.parseDouble(s_latitude);
            double longitude = Double.parseDouble(s_longitude);
            int speed = Integer.parseInt(curTAB.getString(5).toString());
            int position = curTAB.getPosition();

            count++;

            System.out.println("Curr     " + latitude + " --- " + longitude
                    + " --- " + speed);

            if (curTAB.moveToNext()) {
                String next_speed = curTAB.getString(5);
                String ss_latitude = curTAB.getString(1);
                String ss_longitude = curTAB.getString(2);
                System.out.println("Next     " + ss_latitude + " --- "
                        + ss_longitude + " --- " + next_speed);
                curTAB.moveToPosition(position);

                double n_latitude = Double.parseDouble(ss_latitude);
                double n_longitude = Double.parseDouble(ss_longitude);

                if (speed > 60) {
                    map.addPolyline(new PolylineOptions()
                            .add(new LatLng(latitude, longitude),
                                    new LatLng(n_latitude, n_longitude))
                            .width(10).color(Color.RED));
                } else if ((speed < 56) && (speed > 31)) {
                    map.addPolyline(new PolylineOptions()
                            .add(new LatLng(latitude, longitude),
                                    new LatLng(n_latitude, n_longitude))
                            .width(10).color(Color.GREEN));
                } else if (speed < 31) {
                    map.addPolyline(new PolylineOptions()
                            .add(new LatLng(latitude, longitude),
                                    new LatLng(n_latitude, n_longitude))
                            .width(10).color(Color.rgb(255, 127, 80)));
                }
            }else{              
                System.out.println("VÉGE");
            }

            line.add(new LatLng(latitude, longitude));

        }

Upvotes: 0

Views: 820

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117354

I'm not familiar with the maps-android-API, but it looks as if you add a new Polyline for each db-entry which will have the same path as the polyline added before plus the new LatLng from the current entry.

The result will be that you only see the polyline created for the last entry which will cover all the other polylines.

I guess you need to set up a new PolylineOptions-object on each iteration and add as Points the LatLngs of the recent entry and of the current entry to get the desired result.

Upvotes: 1

Related Questions