Java 如何在Android版谷歌地图上显示路线
我有一个应用程序,它通过com.google.maps.DirectionsApi类检索路由列表,并在类型为的对象中返回路由Java 如何在Android版谷歌地图上显示路线,java,android,google-maps,google-maps-api-3,Java,Android,Google Maps,Google Maps Api 3,我有一个应用程序,它通过com.google.maps.DirectionsApi类检索路由列表,并在类型为的对象中返回路由 com.google.maps.model.DirectionsResult,如何在谷歌地图活动中显示此对象中包含的路线?我知道您正在使用 您应该在com.google.maps.model.DirectionsResult的结构中循环,以获取所有LatLng并用于在地图上绘制路线 com.google.maps.model.DirectionsResult包含一个路由数
com.google.maps.model.DirectionsResult,如何在谷歌地图活动中显示此对象中包含的路线?我知道您正在使用 您应该在com.google.maps.model.DirectionsResult的结构中循环,以获取所有LatLng并用于在地图上绘制路线 com.google.maps.model.DirectionsResult包含一个路由数组(DirectionsRoute)。com.google.maps.model.DirectionsRoute包含一个分支数组(DirectionsLeg)。com.google.maps.model.DirectionsLeg包含一组步骤(DirectionsStep)。com.google.maps.model.DirectionsStep包含编码的多段线。您可以从每个编码的多段线获取板条列表,并使用它来构造多段线对象 代码片段可能类似于
for (int i=0; i<result.routes.length; i++) {
DirectionsRoute route = result.routes[i];
PolylineOptions polyOptions = new PolylineOptions();
for (int j=0; j<route.legs.length; j++) {
DirectionsLeg leg = route.legs[j];
for (int k=0; k<leg.steps.length; k++) {
DirectionsStep step = leg.steps[k];
EncodedPolyline encpoly = step.polyline;
List<LatLng> points = encpoly.decodePath();
polyOptions.addAll(points);
}
}
Polyline p = googleMap.addPolyline(polyOptions);
}
for(int i=0;i此方法是实用程序库的一部分。该库是开源的,您可以在Github上看到源代码:您也可以在此处看到更详细的答案: