Reputation: 300
Sorry about my English grammar ! If u can't understand something, free to ask me again !
I use Volley to draw a path between two point in Google Map
private void getPath(LatLng origin, LatLng dest) {
// Origin of route
String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
// Destination of route
String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = str_origin + "&" + str_dest + "&" + sensor;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
showDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
DirectionsJSONParser parser = new DirectionsJSONParser();
routes = parser.parse(response);
List<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routes
for (int i = 0; i < routes.size(); i++) {
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = routes.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(2);
lineOptions.color(Color.RED);
}
if (mGoogleMap != null) {
// Drawing polyline in the Google Map for the i-th route
mGoogleMap.addPolyline(lineOptions);
}
hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("APP", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hideDialog();
}
});
// Adding request to request queue
App.getInstance().addToRequestQueue(jsonObjReq);
}
This code works correctly. But now, I have a list of point (Numbers of point > 2).
How can I draw a path through all points.
I try to write an Asyntask and in doInBackground() function I loop "getPath(x1, x2)" but it's not work.
In my understanding : volley run in background, put it inside an Asyntask (also run in background ) ????!!!!!!
Upvotes: 2
Views: 5509
Reputation: 652
First of all bind click listener on your map
googleMap.setOnMapClickListener(this);
Then, add all point in a list and then draw polyline b/w them.As I am doing below
@Override
public void onMapClick(LatLng latlan) {
latLang.add(latlan);
GeoPoint point = new GeoPoint(latlan.latitude, latlan.longitude);
listPoints.add((IGeoPoint) point);
MarkerOptions marker = new MarkerOptions().position(latlan);
googleMap.addMarker(marker);
if (latLang.size() > 1) {
PolylineOptions polyLine = new PolylineOptions().color(
Color.BLUE).width((float) 7.0);
polyLine.add(latlan);
LatLng previousPoint = latLang.get(latLang.size() - 2);
polyLine.add(previousPoint);
googleMap.addPolyline(polyLine);
}
}
Upvotes: 0