如何存储两个位置之间的路径?

时间:2015-10-26 05:11:06

标签: android sqlite android-layout android-mapview

我正在使用谷歌V2库创建应用程序并放置api。我能够完美地完成任务。但我的下一个任务是在我的数据库中存储源到目标之间的路径,以便用户以后再次访问相同的路径,然后他们不需要再次在地图上搜索。他们可以直接从数据库访问相同的路径。但我不知道我能做到吗。我坚持过去5天我尝试了很多,但没有得到任何解决方案。请帮助我们。提前谢谢:)

1 个答案:

答案 0 :(得分:1)

为了保存db中的所有位置,请使用此代码,希望这对您有所帮助

public void addLocationToDB(Location location){

  if (mDB == null) {
       mDB = new DatabaseHandler(this);
    }
  mDB.addLocation(location);
}

public void addLocation(Location location) {
        try {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_LATITUDE, location.getLatitude());
            values.put(KEY_LONGITUDE, location.getLongitude());

            // Inserting Row
            db.insert(TABLE_LOCATION, null, values);
            db.close(); // Closing database connection
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Insert Exception", "Data base insertion exception");
        }
    }

并且为了从DB获取位置和o在地图上显示使用此

public List<LatLng> getLatLngList() {
        List<LatLng> latLngList = new ArrayList<LatLng>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_LOCATION;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                LatLng latLng = new LatLng(cursor.getDouble(1), cursor.getDouble(2));
                latLngList.add(latLng);

            } while (cursor.moveToNext());
        }
        return latLngList;
    }


public void displayRoutesOnMap() {

   List<LatLng> userLocationList = mDB.getLatLngList();
   ListIterator<LatLng> listIterator = userLocationList.listIterator();
   List<LatLng> points = new ArrayList<LatLng>();
   PolylineOptions polylineOptions = new PolylineOptions();
   polylineOptions.color(Color.RED);
   polylineOptions.width(3);
   int size = 0;
   while (listIterator.hasNext()) {
        LatLng userLocation = listIterator.next();
            points.add(userLocation);
    }
   mGoogleMap.clear();
   size = userLocationList.size();
//        LatLng latLng = points.get(size - 3);
//        tripLastLocation = latLng;
//        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 2000));
    polylineOptions.addAll(points);
    mGoogleMap.addPolyline(polylineOptions);

}