我的应用已经实现了在用户位置到点之间绘制路线的选项。
现在我需要调整5个位置的代码并在它们之间绘制地图
我的代码:
public void getRoute(final LatLng origin, final LatLng destination) {
new Thread() {
public void run() {
int l = 0;
l++;
String url = "http://maps.googleapis.com/maps/api/directions/json?origin="
+ origin.latitude + "," + origin.longitude + "&destination="
+ destination.latitude + "," + destination.longitude + "&sensor=true&mode=walking&alternatives=true®ion=pt";
HttpResponse response;
HttpGet request;
AndroidHttpClient client = AndroidHttpClient.newInstance("route");
request = new HttpGet(url);
try {
response = client.execute(request);
final String answer = EntityUtils.toString(response.getEntity());
runOnUiThread(new Runnable() {
public void run() {
try {
list = buildJSONRoute(answer);
drawRoute();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public List<LatLng> buildJSONRoute(String json) throws JSONException {
JSONObject result = new JSONObject(json);
JSONArray routes = result.getJSONArray("routes");
JSONArray steps = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
List<LatLng> lines = new ArrayList<LatLng>();
for (int i = 0; i < steps.length(); i++) {
String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points");
for (LatLng p : decodePolyline(polyline)) {
lines.add(p);
}
}
return (lines);
}
// Line
private List<LatLng> decodePolyline(String encoded) {
List<LatLng> listPoints = 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)));
Log.i("Script", "POL: LAT: " + p.latitude + " | LNG: " + p.longitude);
listPoints.add(p);
}
return listPoints;
}
画一条路线
public void drawRoute() {
PolylineOptions po;
if (this.polyline == null) {
po = new PolylineOptions();
int i = 0;
for (int tam = list.size(); i < tam; ++i) {
po.add(list.get(i));
}
po.color(Color.BLACK);
this.polyline = this.map.addPolyline(po);
} else {
this.polyline.setPoints(list);
}
}
有任何建议如何调整代码? 此代码可以很好地绘制用户位置和点位置之间的路径
我需要做这样的事情:
答案 0 :(得分:1)
您已经在代码中设置了原点和目的地点,接下来是在它们之间添加航点。 Here是如何调整代码以使用路标制作网址。
似乎某人已经实施了多个积分here。 来自链接的相关代码段。
Route.java
private String makeURL (ArrayList<LatLng> points, String mode, boolean optimize){
StringBuilder urlString = new StringBuilder();
if(mode == null)
mode = "driving";
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
urlString.append("?origin=");// from
urlString.append( points.get(0).latitude);
urlString.append(',');
urlString.append(points.get(0).longitude);
urlString.append("&destination=");
urlString.append(points.get(points.size()-1).latitude);
urlString.append(',');
urlString.append(points.get(points.size()-1).longitude);
urlString.append("&waypoints=");
if(optimize)
urlString.append("optimize:true|");
urlString.append( points.get(1).latitude);
urlString.append(',');
urlString.append(points.get(1).longitude);
for(int i=2;i<points.size()-1;i++)
{
urlString.append('|');
urlString.append( points.get(i).latitude);
urlString.append(',');
urlString.append(points.get(i).longitude);
}
urlString.append("&sensor=true&mode="+mode);
return urlString.toString();
}