[英]How to draw route and calculate distance between multiple markers on google map in android
In my app user can insert multiple location and show in map. 在我的应用中,用户可以插入多个位置并在地图中显示。 How can i achieve this? 我怎样才能实现这一目标? I know how to draw route between two location but i want to draw route between multiple marker as like image. 我知道如何在两个位置之间绘制路线,但我想在多个标记之间绘制路线,就像图像一样。
In image marker show location that is entered by user. 在图像标记显示用户输入的位置。 I also want to calculate distance between marker like calculate distance between B to C and C to D. 我还想计算标记之间的距离,比如计算B到C和C到D之间的距离。
How can I achieve this?? 我怎么能实现这个?
Use direction api which return multi-part directions using a series of waypoints. 使用方向api,使用一系列航路点返回多部分方向。
Direction api Documentation 方向api文档
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;
}
}
When you get response from above request . 当您收到上述请求的回复时。 you need to draw route from response 你需要从响应中画出路线
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) {
}
}
You will get more detail of this from Draw-route-github 您将从Draw-route-github获得更多细节
For Distance calculation you need to Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations 对于距离计算,您需要距离矩阵API是一种服务,它为起始和目的地矩阵提供行程距离和时间
Using direction api you can achieve this. 使用方向api你可以实现这一目标。 you just have to pass user inserted marker as waypoints as follow 您只需将用户插入的标记作为路径传递如下
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
you will get list of route which have the distance between to points 您将获得具有点到点之间距离的路线列表
//retrofit //改造
@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);
// plotting logic //绘制逻辑
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() {
}
});
}
}
Google provides libraries out of the box for solving this kind of issues. Google提供了开箱即用的库来解决此类问题。
I would structure my application in following manner. 我会按照以下方式构建我的应用程序。
Use google API's, you will need more than 1 API to achieve both of the tasks. 使用谷歌API,您将需要超过1个API来实现这两项任务。
2.a To find out distances between two points use Google Distance Matrix API ( https://developers.google.com/maps/documentation/distance-matrix/start ). 2.a要查找两点之间的距离,请使用Google Distance Matrix API( https://developers.google.com/maps/documentation/distance-matrix/start )。
2.b To add multiple markers you can refer to following answer Google Maps JS API v3 - Simple Multiple Marker Example 2.b要添加多个标记,您可以参考以下答案Google Maps JS API v3 - 简单多标记示例
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**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.