当前位置: 代码迷 >> Android >> 如何存储两个位置之间的路径?
  详细解决方案

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

热度:80   发布时间:2023-08-04 12:00:02.0

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

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

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

}
  相关解决方案