我在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;
任何人都可以在多个标记的绘制路径中给我一些教程或指导吗?提前谢谢!
答案 0 :(得分:7)
首先,您需要修复方法
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;
}
答案 1 :(得分:0)
有时它会在错误的网址生成时发生使用点数.. 愿这个功能有用。 它对我有用,所以我分享了......
public static String getMapsApiDirectionsUrl( LatLng start, LatLng End ) {
String waypoints = "waypoints=optimize:true|"
+ start.latitude + "," + start.longitude
+ "|" + "|" + End.latitude + ","
+ End.longitude;
String OriDest = "origin="+start.latitude+","+start.longitude+"&destination="+End.latitude+","+End.longitude;
String sensor = "sensor=false";
String params = OriDest+"&%20"+waypoints + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + params;
return url;
}
感谢。