i fetched points from this url just for test :
https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey
i run this AsyncTask class in order to get and draw directions, this class in used in Map class extending AppCompatActivity:
private class direction extends AsyncTask<String, Void, String> {
private Context mContext;
List<LatLng> pontos;
ProgressDialog dialog;
double currentLatitude, currentLongitude,destinationLatitude,destinationLongitude;
String walkDistance;
public direction (Context context,LatLng point){
mContext = context;
destinationLatitude = point.latitude;
destinationLongitude = point.longitude;
}
@Override
protected String doInBackground(String... params) {
JSONObject obj;
String response = HttpRequest.get("https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey").body();
try {
System.out.println("Response content 1 was " + response);
String jsonOutput = response.toString();
JSONObject jsonObject = new JSONObject(jsonOutput);
// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject firtsLegs = legs.getJSONObject(0);
JSONObject distance = firtsLegs.getJSONObject("distance");
System.out.println("Response test was : " + distance.getString("text"));
walkDistance = distance.getString("text");
//Toast.makeText(getApplicationContext(), "????? ??? ?? ????: " + distance.getString("text"), Toast.LENGTH_LONG).show();
JSONObject poly = route.getJSONObject("overview_polyline");
String polyline = poly.getString("points");
pontos = decodePoly(polyline);
return response;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "distance : "+walkDistance, Toast.LENGTH_LONG).show();
/*System.out.println("Response content 2 was " + result);
Polygon polygon = mapFragment.addPolygon(new PolygonOptions()
.add(new LatLng(35.7513974, 51.4350038),
new LatLng(35.7713974, 51.4350038),
new LatLng(35.7913974, 51.4350038),
new LatLng(35.7913974, 51.5350038))
.strokeWidth(5)
.strokeColor(Color.BLUE));*/
for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
try{
//here is where it will draw the polyline in your map
/*Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(10).color(Color.BLUE).geodesic(false));
*/
Polygon line = googleMap.addPolygon(new PolygonOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.strokeColor(Color.BLUE).geodesic(true));
}catch(NullPointerException e){
Log.e("Error", "NullPointerException onPostExecute: " + e.toString());
}catch (Exception e2) {
Log.e("Error", "Exception onPostExecute: " + e2.toString());
}
}
dialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(Map.this);
dialog.setMessage("?? ??? ???? ???? ???? ??? ?????? ???? ??? ????.");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
/*gps = new GPSTracker(Map.this);
// Check if GPS enabled
if(gps.canGetLocation()) {
currentLatitude = gps.getLatitude();
currentLongitude = gps.getLongitude();
//
is for new line
Toast.makeText(getApplicationContext(), "Your Location is -
Lat: " + currentLatitude + "
Long: " + currentLongitude, Toast.LENGTH_LONG).show();
} else {
// Can't get location.
// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.
gps.showSettingsAlert();
}*/
}
@Override
protected void onProgressUpdate(Void... values) {}
}
but lines are not on the road and it looks like bellow image :
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…