gpt4 book ai didi

android - 如何在android中的谷歌地图上绘制路线并计算多个标记之间的距离

转载 作者:太空狗 更新时间:2023-10-29 13:45:46 24 4
gpt4 key购买 nike

在我的应用程序中,用户可以插入多个位置并在 map 中显示。我怎样才能做到这一点?我知道如何在两个位置之间绘制路线,但我想像图像一样在多个标记之间绘制路线。 enter image description here

在图像标记中显示用户输入的位置。我还想计算标记之间的距离,例如计算 B 到 C 和 C 到 D 之间的距离。

我怎样才能做到这一点??

最佳答案

使用方向 api,它使用一系列路标返回多部分方向。

Direction api Documentation

private static final LatLng LOWER_MANHATTAN = new LatLng(40.722543,-73.998585);
private static final LatLng BROOKLYN_BRIDGE = new LatLng(40.7057, -73.9964);
private static final LatLng WALL_STREET = new LatLng(40.7064, -74.0094);

private String getMapsApiDirectionsUrl() {
String origin = "origin=" + LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude;
String waypoints = "waypoints=optimize:true|" + BROOKLYN_BRIDGE.latitude + "," + BROOKLYN_BRIDGE.longitude + "|";
String destination = "destination=" + WALL_STREET.latitude + "," + WALL_STREET.longitude;

String sensor = "sensor=false";
String params = origin + "&" + waypoints + "&" + destination + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + params;
return url;
}
}

当您收到上述请求的回复时。你需要从响应中绘制路线

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) {

}
}

您将从 Draw-route-github 获得更多详细信息

对于距离计算,您需要 Distance Matrix API 是一项为起点和终点矩阵提供行程距离和时间的服务

关于android - 如何在android中的谷歌地图上绘制路线并计算多个标记之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54938349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com