Issue
i am creating app by using google V2 library and place api. i am able to do eveything perfectly.but my next task is to store the path between source to destination in my database for the purpose if user wants to access the same path again in future then they should not need to search again on map. they can directly access the same path from database. but i have no idea about it ho can i do it. i stuck on it from last 5 days i tried a lot but not getting any solution.please help me guys. thanx in advance :)
Solution
In order to save all location in db use this code hope this will help you
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");
}
}
and in Order to get Location from DB and o Show on Map use this
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);
}
Answered By - Mustanser Iqbal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.