谷歌地图v2绘制折线取决于速度

时间:2013-08-16 14:48:14

标签: android google-maps

我有这个数据库: enter image description here

我尝试制作一个简单的折线图,这取决于速度:

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

但每条线都是相同的颜色。 请帮帮我,我做错了什么!

更新: 工作代码(在最后一点它不起作用,但不是一个大问题)

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

        }

1 个答案:

答案 0 :(得分:1)

我不熟悉maps-android-API,但看起来好像你为每个db-entry添加一个新的Polyline,它与 plus 之前添加的折线具有相同的路径来自当前条目的新LatLng。

结果将是您只能看到为最后一个条目创建的折线,它将覆盖所有其他折线。

我猜你需要在每次迭代时设置一个新的PolylineOptions对象,并将最近一个条目和当前条目的LatLngs作为Points添加,以获得所需的结果。

相关问题