【问题标题】:how to store the path between two locations?如何存储两个位置之间的路径?
【发布时间】:2015-10-26 05:11:06
【问题描述】:

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

【问题讨论】:

  • 获取两个位置的 lat lang 并将它们以 Double 或 string 的形式存储在数据库或 Shared Preference 中,当您想要显示时,只需从数据库获取并将其传递给 lat lant,如 LatLang ltlng =新的 LatLang(存储的 lat,存储的 lang)..就是这样
  • @MustanserIqbal 谢谢回复。我尝试了相同的方法,但由此可以在源和目的地之间有许多路线。我想存储相同的路线意味着来源和目的地之间的位置的纬度也存储在数据库中。
  • 好的,让我来回答您如何将所有路由保存在数据库中。.
  • 请检查我的答案
  • 是的,我试过很多。

标签: android sqlite android-layout android-mapview


【解决方案1】:

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

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

为了从数据库中获取位置并在地图上显示使用这个

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多