Dưới đây là một số mã giúp bạn.
String url=
"http://maps.googleapis.com/maps/api/directions/json?origin="
+ origin.latitude + "," + origin.longitude +"&destination="
+ destination.latitude + "," + destination.longitude + "&sensor=false";
Để lấy dữ liệu với androidhttpclient, làm một cái gì đó như thế này:
HttpResponse response;
HttpGet request;
AndroidHttpClient client = AndroidHttpClient.newInstance("somename");
request = new HttpGet(url);
response = client.execute(request);
InputStream source = response.getEntity().getContent();
String returnValue = buildStringIOutils(source);
return returnValue;
nơi buildStringIOUtils là:
private String buildStringIOutils(InputStream is) {
try {
return IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Sau đó bạn có thể trích xuất các polyline thực tế từ JSON-phản ứng với một cái gì đó như thế này:
JSONObject result = new JSONObject(returnValue);
JSONArray routes = result.getJSONArray("routes");
long distanceForSegment = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("value");
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);
}
}
nơi phương pháp decodePolyline là thế này:
/** POLYLINE DECODER - http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java **/
private List<LatLng> decodePolyline(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;
}
Sau đó, bạn có thể thêm các polyline vào bản đồ với điều này:
Polyline polylineToAdd = mMap.addPolyline(new PolylineOptions().addAll(lines).width(3).color(Color.RED));
Để thay đổi chế độ, thêm video này vào url (Xem https://developers.google.com/maps/documentation/directions/): & chế độ = YOUR_MODE
lái xe (mặc định) cho biết chỉ đường lái xe tiêu chuẩn sử dụng mạng lưới đường bộ.
yêu cầu đi bộ chỉ đường đi bộ qua đường dành cho người đi bộ & vỉa hè (nếu có).
yêu cầu đi xe đạp chỉ đường đi xe đạp qua đường dành cho xe đạp & các đường phố ưa thích (nếu có).
chỉ đường yêu cầu chuyển tuyến qua các tuyến chuyển tuyến công cộng (nếu có).
Chỉnh sửa: Giới thiệu về "Tôi cũng muốn nhận thông tin giao thông như các tuyến đường bận rộn, tắc nghẽn, v.v ..." Tôi đã không nhìn vào điều này, nhưng mã của tôi sẽ giúp bạn bắt đầu khá tốt.
Chỉnh sửa2: Tìm thấy điều này trong chỉ đường google api: "Chỉ đường lái xe: Khách hàng Maps for Business có thể chỉ định thời gian khởi hành để nhận thời gian chuyến đi xem xét điều kiện giao thông hiện tại. thời điểm hiện tại."
Tôi đi theo này http://stackoverflow.com/questions/11745314/why-retrieving-google-directions-for-android-using-kml-data -is-không-làm việc-anymo để có được các tuyến đường. Đó là bước đầu tiên. – Stochastically