我在 Uni 校园里做关于公交车跟踪器的 android 项目。我需要绘制一条从一个巴士站到另一个巴士站的路线,直到回到起点巴士站。(我的 Uni 大约有 20 个巴士站,需要绘制路线让第一个用户知道巴士的路线)。
我按照本教程设法绘制了一条路径 here
在另一个教程中,我发现了一个博客,该博客教授在 3 个位置的谷歌地图上绘制路径。示例代码为
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 waypoints = "waypoints=optimize:true|"
+ LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude
+ "|" + "|" + BROOKLYN_BRIDGE.latitude + ","
+ BROOKLYN_BRIDGE.longitude + "|" + WALL_STREET.latitude + ","
+ WALL_STREET.longitude;
String sensor = "sensor=false";
String params = waypoints + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + params;
return url;
}
但是我得到了一个错误:
java.io.FileNotFoundException: https://maps.googleapis.com/maps/api/directions/json?waypoints=optimize:true|2.920114,101.775244||2.919726,101.771172|2.92234,101.769628&sensor=false
E/Background Task: java.lang.NullPointerException
E/ParserTask: errororg.json.JSONException: End of input at character 0 of
博客的教程与链接相同。只是不一样
String waypoints = "waypoints=optimize:true|"
+ LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude
+ "|" + "|" + BROOKLYN_BRIDGE.latitude + ","
+ BROOKLYN_BRIDGE.longitude + "|" + WALL_STREET.latitude + ","
+ WALL_STREET.longitude;
和
/ Origin of route
String str_origin = "origin="+origin.latitude+","+origin.longitude;
// Destination of route
String str_dest = "destination="+dest.latitude+","+dest.longitude;
任何人都可以给我一些关于在多个标记中绘制路径的教程或指导吗?提前致谢!
最佳答案
首先你需要修正你的方法
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;
}
为了获得绘图路径,我遵循了这个答案 https://stackoverflow.com/a/14702636/2697368
一旦你得到JSON,就去做
public void drawPath(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) {
}
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
关于android - 根据我自己在 android Google maps v2 中的标记绘制路线路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36456589/