如何存储两个位置之间的路径?
how to store the path between two locations?
我正在使用 google V2 库创建应用程序并放置 api。我可以做所有事情 perfectly.but 我的下一个任务是将源到目的地之间的路径存储在我的数据库中,如果用户将来想再次访问相同的路径,那么他们就不需要在地图上再次搜索.他们可以直接从数据库访问相同的路径。但我不知道我能做到吗?我从过去 5 天开始就坚持使用它,我尝试了很多但没有得到任何 solution.please 帮助我的人。提前感谢 :)
为了在数据库中保存所有位置,请使用此代码希望这对您有所帮助
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);
}
我正在使用 google V2 库创建应用程序并放置 api。我可以做所有事情 perfectly.but 我的下一个任务是将源到目的地之间的路径存储在我的数据库中,如果用户将来想再次访问相同的路径,那么他们就不需要在地图上再次搜索.他们可以直接从数据库访问相同的路径。但我不知道我能做到吗?我从过去 5 天开始就坚持使用它,我尝试了很多但没有得到任何 solution.please 帮助我的人。提前感谢 :)
为了在数据库中保存所有位置,请使用此代码希望这对您有所帮助
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);
}