2
我的應用已經實現了在用戶位置與點之間繪製路線的選項。如何使用googlemapsApi繪製2條或更多路線
現在我需要用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);
}
}
任何建議如何適應碼? 此代碼工作正常繪製用戶位置和點位置之間的路線
我需要做這樣的事情: