1

Hi am developing one application in that am displaying map with current location and longitude and latitude values and change that location. every 5 minutes am storing these latitude and longitude values in sqlitedatabase and am getting these database values its working fine. but, how to draw path using stored database values. please anybody tell me the solution.

code:

    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.color(Color.RED);
    polylineOptions.width(5);
    double latitude=Double.parseDouble(c.getString(0));
    double longitude=Double.parseDouble(c.getString(1));

    LatLng latlngval=new LatLng(latitude, longitude);
    Log.e("path", ""+latlngval);
    ArrayList<LatLng> latlnglist=new ArrayList<LatLng>();
    latlnglist.add(latlngval);

    for (int i = 0; i < latlnglist.size(); i++) {

        polylineOptions.add(latlnglist.get(i));

    }
user3612165
  • 43
  • 1
  • 12

3 Answers3

1

You can use Polyline

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.RED);
polylineOptions.width(5);
Cursor c = database.query( ... ); //select the latitude and longitude column
while (c.moveToNext()) {
    polylineOptions.add(new LatLng(Double.parseDouble(c.getString(0)), Double.parseDouble(c.getString(1))));
}
googleMap.addPolyline(polylineOptions);
Wilik
  • 7,630
  • 3
  • 29
  • 35
0
public class DrawrootTask extends AsyncTask<String, String, String> {
private Context context;
public static boolean flagCompleted = false;
private GoogleMap googleMap;
private double source_lat = 0.0;
private double source_long = 0.0;
private double dest_lat = 0.0;
private double dest_long = 0.0;
Userdata userdata;
String tag = "DrawRootTask";
private ProgressDialog progressDialog;
public static double dist, time;
private Polyline line;
String distanceText = "";
String durationText = "";

public DrawrootTask(Context context, LatLng source, LatLng destination,
        GoogleMap googleMap) {
    source_lat = source.latitude;
    source_long = source.longitude;
    dest_lat = destination.latitude;
    dest_long = destination.longitude;

    this.googleMap = googleMap;
    this.context = context;
    userdata = Userdata.getinstance(context);

}

protected void onPreExecute() {
    // // TODO Auto-generated method stub
    super.onPreExecute();
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage(context.getResources().getString(
            R.string.please_wait));
    progressDialog.setIndeterminate(true);
    progressDialog.show();

}

@Override
protected String doInBackground(String... params) {

    String json = "";

    // constructor
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    HashMap<String, String> keyValue = new HashMap<String, String>();
    urlString.append("?origin=");// from
    urlString.append(Double.toString(source_lat));
    urlString.append(",");
    urlString.append(Double.toString(source_long));
    urlString.append("&destination=");// to
    urlString.append(Double.toString(dest_lat));
    urlString.append(",");
    urlString.append(Double.toString(dest_long));
    urlString.append("&sensor=false&mode=driving&alternatives=true");

    // defaultHttpClient
    String url = urlString.toString();
    FetchUrl fetchurl = new FetchUrl();
    json = fetchurl.fetchUrl(url, keyValue);

    Log.e("Buffer Error", json);
    return json;

}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    try {
        progressDialog.dismiss();
        final JSONObject json = new JSONObject(result);
        JSONArray routeArray = json.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes
                .getJSONObject("overview_polyline");
        String encodedString = overviewPolylines.getString("points");
        List<LatLng> list = decodePoly(encodedString);

        for (int z = 0; z < list.size() - 1; z++) {
            LatLng src = list.get(z);
            LatLng dest = list.get(z + 1);
            line = googleMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude, dest.longitude))
                    // .width(8).color(Color.BLUE).geodesic(true));
                    .width(8)
                    .color(context.getResources().getColor(
                            R.color.actionbar_color)).geodesic(true));
            Log.i("draw root", "" + "" + line.toString());
        }
        JSONArray legs = routes.getJSONArray("legs");
        JSONObject steps = legs.getJSONObject(0);
        JSONObject duration = steps.getJSONObject("duration");
        JSONObject distance = steps.getJSONObject("distance");
        distanceText = distance.getString("text");
        durationText = duration.getString("text");
        Log.i("draw root", "" + distance.toString());
        dist = Double.parseDouble(distance.getString("text").replaceAll(
                "[^\\.0123456789]", ""));
        time = Double.parseDouble(duration.getString("text").replaceAll(
                "[^\\.0123456789]", ""));
        userdata.setDistance(dist);
        userdata.setTime(time);
        Log.d(tag, "distace is " + dist + " time is " + time);
        flagCompleted = true;
    } catch (JSONException e) {
        Log.d("draw root", "" + e);

    }
}

private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
        poly.add(p);
    }

    return poly;
}

}

Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15
0

may be this will help you..

http://javapapers.com/android/draw-lines-on-google-maps-android-api/

instead of taking hardcoded values(as in the example) retrieve the values from your database

Maniya Joe
  • 761
  • 6
  • 24