我从这个网址中获取了一些仅用于测试的点数:
https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey
我运行这个AsyncTask类以获取和绘制方向,这个类在Map类中用于扩展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();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + currentLatitude + "\nLong: " + 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) {}
}
但路线不在路上,它看起来像波纹管图像:
答案 0 :(得分:2)
您正在使用返回的overview_polyline
对象中的routes
来绘制路径。正如the documentation所说(强调我的)
overview_polyline包含一个单点对象,该对象包含路径的编码折线表示。 此折线是生成路线的近似(平滑)路径。
如果您绘制polyline
数组中收到的每个step
的{{1}}对象,您将得到更好的近似值。
在您的示例中(红线表示legs
,蓝线表示收到的第一个overview_polyline
的{{1}}对象: