0

I am developing an android application in this application i want to draw path line on map

As user move path is draw automatically i found the lat and long and its change every time

When location is changed so i want to draw line on map when every time location is changed

i tried this

public void onLocationChanged(Location location) {

    String msg = "Location:" + location.getLatitude() + ","
            + location.getLongitude();
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    double new_lat = location.getLatitude();
    double new_long = location.getLongitude();
    drawTrack(new_lat, new_long, previous_lat, previous_long);
    previous_lat = new_lat;
    previous_long = new_long;
}
Mahtab
  • 269
  • 3
  • 11

1 Answers1

2

This is what I did:

public void onLocationChanged(Location location) {
    prevLatitude = latitude;
    prevLongitude = longitude;
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    latLng = new LatLng(latitude, longitude);

    PolylineOptions pOptions = new PolylineOptions()
        .width(5)
        .color(Color.GREEN)
        .geodesic(true);
    for (int z = 0; z < routePoints.size(); z++) {
        LatLng point = routePoints.get(z);
        pOptions.add(point);
    }
    line = googleMap.addPolyline(pOptions);
    routePoints.add(latLng);
}

in which routePoints is an arraylist of LatLng's, line is your Polyline and googleMap is, ofcourse, your GoogleMap. So on the beginning of the class add:

private GoogleMap googleMap;
private Polyline line;
private ArrayList<LatLng> routePoints;

and in the onCreate add:

routePoints = new ArrayList<LatLng>();

hope this helps for you!:)

Matthijs
  • 71
  • 8
  • private void drawTrack(double new_lat, double new_long, double previous_lat, double previous_long) { PolylineOptions options = new PolylineOptions(); options.add(new LatLng(previous_lat, previous_long)); options.add(new LatLng(new_lat, new_long)); options.width(10); options.color(Color.RED); mMap.addPolyline(options); } – Mahtab Jun 26 '14 at 07:16
  • this is my drawtrack method todd – Mahtab Jun 26 '14 at 07:16
  • Not in the context of my application, but this might work. Do realise you add every latLng twice, assuming you do `previous_lat = new_lat` and the same for the longitude outside of the method but within the locationListener. I suggest making the LatLngs before calling the drawTrack method and taking the newest one as a parameter of the drawTrack method so you can just add that one and have 1 LatLng for every location. I remember trying to draw a line like that, but it didn't work for me. If this does not work for you, you could try adding an array of LatLngs and using the for-loop. – Matthijs Jun 26 '14 at 08:37