如何使用 Alternatives=true 从 Google Maps 路线中找出最短路线?
Posted
技术标签:
【中文标题】如何使用 Alternatives=true 从 Google Maps 路线中找出最短路线?【英文标题】:How do I find out the shortest route from Google Maps directions with alternatives=true? 【发布时间】:2018-11-01 23:17:09 【问题描述】:Google 路线 API:https://maps.googleapis.com/maps/api/directions/json?origin=1.29068,103.851743&destination=1.324298,103.9032129&sensor=false&mode-driving&alternatives=true
链接中有 3 条路线,但我必须编辑我的代码以处理和显示所有 3 条路线。我正在寻找最短的路线,避免过路费的路线,以及最快的路线(我相信这是google提供的默认路线)
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... strings)
JSONObject jsonObject;
List<List<HashMap<String, String>>> routes = null;
try
jsonObject = new JSONObject(strings[0]);
DirectionsJSONParser directionsJSONParser = new DirectionsJSONParser();
routes = directionsJSONParser.parse(jsonObject);
catch (JSONException e)
e.printStackTrace();
return routes;
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> lists)
//Get list route and display
ArrayList points = null;
PolylineOptions polylineOptions = null;
String distance = "";
String duration = "";
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (int i = 0; i < lists.size(); i++)
points = new ArrayList();
polylineOptions = new PolylineOptions();
List<HashMap<String, String>> path = lists.get(i);
for (int j = 0; j < path.size(); j++)
HashMap<String, String> point = path.get(j);
if(j==0) // Get distance from the list
distance = point.get("distance");
continue;
else if(j==1) // Get duration from the list
duration = point.get("duration");
continue;
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
boundsBuilder.include(position);
polylineOptions.addAll(points);
polylineOptions.width(12);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);
if (polylineOptions != null)
mMap.addPolyline(polylineOptions);
LatLngBounds routeBounds = boundsBuilder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(routeBounds, 12));
mDisTextView.setText(distance);
mDuraTextView.setText(duration);
else
Toast.makeText(MapActivity.this, "Directions not found!", Toast.LENGTH_SHORT).show();
这是我的 JSONParser 类
public List<List<HashMap<String,String>>> parse(JSONObject jObject)
List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
JSONArray jRoutes = null;
JSONArray jLegs = null;
JSONArray jSteps = null;
JSONObject jDistance = null;
JSONObject jDuration = null;
try
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for(int i=0;i<jRoutes.length();i++)
jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
List path = new ArrayList<HashMap<String, String>>();
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++)
/** Getting distance from the json data */
jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");
HashMap<String, String> hmDistance = new HashMap<String, String>();
hmDistance.put("distance", jDistance.getString("text"));
/** Getting duration from the json data */
jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
HashMap<String, String> hmDuration = new HashMap<String, String>();
hmDuration.put("duration", jDuration.getString("text"));
/** Adding distance object to the path */
path.add(hmDistance);
/** Adding duration object to the path */
path.add(hmDuration);
jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
/** Traversing all steps */
for(int k=0;k<jSteps.length();k++)
String polyline = "";
polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
List list = decodePoly(polyline);
/** Traversing all points */
for(int l=0;l <list.size();l++)
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) );
hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) );
path.add(hm);
routes.add(path);
catch (JSONException e)
e.printStackTrace();
catch (Exception e)
return routes;
/**
* Method to decode polyline points
* Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
* */
private List decodePoly(String encoded)
List poly = new ArrayList();
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;
【问题讨论】:
【参考方案1】:我假设如果您将替代项设置为 true,则 API 将返回多个路由对象。假设您只需要遍历路由对象数组并根据它们的总长度(距离)对它们进行排序。
【讨论】:
谢谢,你是对的。我对java的理解仍然相当有限,我想我应该编辑 onPostExecute 方法吧?你有什么可以借鉴的例子吗? 不幸的是,我根本没有用 Java 编码。但是,我经常使用 Google Directions API。【参考方案2】:-默认情况下,如果您将此键设置为 false,Google 会为您提供最短路径 google 会为您提供最短路径alternatives=false
如果你想要替代品,那么你必须在下面设置方向 API 的键:
-不幸的是,目前没有办法让 API 在所有情况下都返回绝对最短的路线。
但使用方向 API 中的 avoid 键,您可以找到最短路线,如下所示:
Google Directions Api:
https://maps.googleapis.com/maps/api/directions/json?origin=1.29068,103.851743&destination=1.324298,103.9032129&avoid=highways&sensor=false&mode-driving&alternatives=true
【讨论】:
有没有办法比较距离并找出最短距离?以上是关于如何使用 Alternatives=true 从 Google Maps 路线中找出最短路线?的主要内容,如果未能解决你的问题,请参考以下文章
centos/linux alternatives与update-alternatives详解与软件版本切换