如何在android中在google地图上绘制路线并计算多个标记之间的距离
在我的应用程序中,用户可以插入多个位置并在地图中显示。我怎样才能做到这一点?我知道如何在两个位置之间绘制路线,但我想像图一样在多个标记之间绘制路线 在图像标记中显示用户输入的位置。我还想计算标记之间的距离,就像计算B到C和C到D之间的距离一样如何在android中在google地图上绘制路线并计算多个标记之间的距离,android,google-maps,marker,Android,Google Maps,Marker,在我的应用程序中,用户可以插入多个位置并在地图中显示。我怎样才能做到这一点?我知道如何在两个位置之间绘制路线,但我想像图一样在多个标记之间绘制路线 在图像标记中显示用户输入的位置。我还想计算标记之间的距离,就像计算B到C和C到D之间的距离一样 我怎样才能做到这一点 使用direction api可以实现这一点。 您只需将用户插入的标记作为航路点传递,如下所示 https://maps.googleapis.com/maps/api/directions/json? origin=sydney,a
我怎样才能做到这一点 使用direction api可以实现这一点。 您只需将用户插入的标记作为航路点传递,如下所示
https://maps.googleapis.com/maps/api/directions/json?
origin=sydney,au&destination=perth,au
&waypoints=via:-37.81223%2C144.96254%7Cvia:-34.92788%2C138.60008
&key=YOUR_API_KEY
您将获得到点之间有距离的路线列表
//改造
@GET("https://maps.googleapis.com/maps/api/directions/json")
Observable<DirectionResults> getDirectionWithWayPoints(@Query("origin") String origin, @Query("destination") String destination, @Query("waypoints") String wayPoints, @Query("key") String key);
@GET(“https://maps.googleapis.com/maps/api/directions/json")
可观测的getDirectionWithWayPoints(@Query(“origin”)字符串origin、@Query(“destination”)字符串destination、@Query(“waypoints”)字符串waypoints、@Query(“key”)字符串key);
//绘图逻辑
api.getDirectionWithWayPoints(startPoint, endPoint, stringBuilder.toString(), getString(R.string.API_KEY))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<DirectionResults>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(DirectionResults directionResults) {
hideDialog();
if (null == directionResults) {
return;
}
ArrayList<LatLng> routelist = new ArrayList<>();
routelist.add(latLngStart);
if (directionResults.getRoutes().size() > 0) {
List<LatLng> decodelist;
RoutesItem routeA = directionResults.getRoutes().get(0);
if (routeA.getLegs().size() > 0) {
for (int j = 0; j < routeA.getLegs().size(); j++) {
List<StepsItem> steps = routeA.getLegs().get(j).getSteps();
StepsItem step;
Location location;
String polyline;
for (int i = 0; i < steps.size(); i++) {
step = steps.get(i);
polyline = step.getPolyline().getPoints();
decodelist = DirectionsJSONParser.decodePoly(polyline);
routelist.addAll(decodelist);
}
}
}
}
if (routelist.size() > 0) {
routelist.add(latLngEnd);
rectLine = new PolylineOptions().width(12).color(
Color.CYAN);
for (int i = 0; i < routelist.size(); i++) {
rectLine.add(routelist.get(i));
}
// Adding route on the map
if (null != mMap) {
mMap.addPolyline(rectLine);
fixZoom(rectLine, mMap);
getVehicleId();
}
}
}
@Override
public void onError(Throwable e) {
hideDialog();
e.printStackTrace();
}
@Override
public void onComplete() {
}
});
}
}
api.getDirectionWithWayPoints(起点、终点、stringBuilder.toString()、getString(R.string.api_键))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribouth(新观察员){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
public void onNext(DirectionResults DirectionResults){
hideDialog();
if(null==方向结果){
返回;
}
ArrayList routelist=新的ArrayList();
路线列表添加(latLngStart);
if(directionResults.getRoutes().size()>0){
列表解码列表;
RouteItem routeA=directionResults.getRoutes().get(0);
如果(routeA.getLegs().size()>0){
对于(int j=0;j0){
路线列表添加(LatlGend);
矩形线=新的多段线选项().宽度(12).颜色(
颜色(青色);
对于(int i=0;i
使用方向api,该api使用一系列航路点返回多部分方向
当您收到上述请求的响应时。你们需要从反应中得出路线
public void drawRoute(String result) {
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
Polyline line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.width(12)
.color(Color.parseColor("#05b1fb"))//Google maps blue color
.geodesic(true)
);
} catch (JSONException e) {
}
}
public void drawRoute(字符串结果){
试一试{
//将字符串转换为json对象
最终JSONObject json=新JSONObject(结果);
JSONArray routarray=json.getJSONArray(“路由”);
JSONObject routes=routeArray.getJSONObject(0);
JSONObject overviewPolylines=routes.getJSONObject(“overview_Polylines”);
字符串编码字符串=概览多段线。获取字符串(“点”);
列表=解码多边形(编码字符串);
Polyline line=mMap.addPolyline(新的Polyline选项()
.addAll(列表)
.宽度(12)
.color(color.parseColor(#05b1fb”)//谷歌地图蓝色
.测地线(真)
);
}捕获(JSONException e){
}
}
您将从中获得更多详细信息
对于距离计算,您需要距离矩阵API是一项为出发地和目的地矩阵提供旅行距离和时间的服务谷歌为解决此类问题提供了现成的库 我将按照以下方式组织我的申请
public void drawRoute(String result) {
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
Polyline line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.width(12)
.color(Color.parseColor("#05b1fb"))//Google maps blue color
.geodesic(true)
);
} catch (JSONException e) {
}
}
For draw route you can use :
PolylineOptions options = new
PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < list.size(); z++) {
LatLng point = list.get(z);
options.add(point);
}
line = myMap.addPolyline(options);
And calculating distance for usimg **Google Maps Direction API**