解决方法很简单
如果将来寻找此类功能,这可能会对某人有所帮助。
该方法在google direction API返回location A to B的路由后调用。 directionPoints 是路径中点的list。
public void handleResult(ArrayList<LatLng> directionPoints)
{
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoints.size() ; i++)
{
rectLine.add(directionPoints.get(i));
}
//this polyline is stored so that it can be removed by calling drawnRoutePath.remove() if needed
drawnRoutePath = googleMap.addPolyline(rectLine);
prepareBuilder(directionPoints);
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));
}
更新
来自 Google Direction API 的 json 响应
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 27.7136953,
"lng" : 85.32216629999999
},
"southwest" : {
"lat" : 27.7103725,
"lng" : 85.3214952
}
},
.... etc
所以我使用这个绑定的lat 和lng 作为LatLngBounds.Builder 的参数。
JSONObject jsonBound = ((JSONObject)jRoutes.get(i)).getJSONObject("bounds");
JSONObject jsonSouthWest = jsonBound.getJSONObject("southwest");
JSONObject jsonNorthEast = jsonBound.getJSONObject("northeast");
LatLng boundSouthWest = new LatLng(jsonSouthWest.getDouble("lat"),jsonSouthWest.getDouble("lng"));
LatLng boundNorthEast = new LatLng(jsonNorthEast.getDouble("lat"),jsonNorthEast.getDouble("lng"));
ArrayList<LatLng> bounds = new ArrayList<LatLng>();
bounds.add(boundNorthEast);
bounds.add(boundSouthWest);
并将这些点包含在LatLngBounds.Builder中