我正在尝试使用最佳路线在两个地方之间绘制一条折线。
源和目标之间有两个或多个点。
有人可以告诉我如何在Android Studio中使用JSONObject / JSONArray在polyline
中获得积分吗?
{
routes": [
{
"bounds": {
"northeast": {
"lat": 17.4961254,
"lng": 78.44918179999999
},
"southwest": {
"lat": 17.4177332,
"lng": 78.3743566
}
},
"copyrights": "Map data ©2019 Google",
"legs": [
{
"distance": {
"text": "10.2 km",
"value": 10201
},
"duration": {
"text": "25 mins",
"value": 1527
},
"end_address": "207, National Highway 65, Near South India Shopping Mall, A.S.Raju Nagar, Vivekananda Nagar, Kukatpally, Hyderabad, Telangana 500072, India",
"end_location": {
"lat": 17.4929352,
"lng": 78.4053682
},
"start_address": "7-1-212/A/49/1, ShivBagh, Ameerpet, Hyderabad, Telangana 500016, India",
"start_location": {
"lat": 17.440171,
"lng": 78.44918179999999
},
"steps": [
{
"distance": {
"text": "89 m",
"value": 89
},
"duration": {
"text": "1 min",
"value": 45
},
"end_location": {
"lat": 17.4393871,
"lng": 78.4489992
},
"html_instructions": "Head <b>south</b> on <b>Shivbagh Colony Road</b>",
"polyline": {
"points": "ahmiBkby}MdAPtAP"
},
"start_location": {
"lat": 17.440171,
"lng": 78.44918179999999
},
"travel_mode": "DRIVING"
},
{
"distance": {
"text": "76 m",
"value": 76
},
"duration": {
"text": "1 min",
"value": 36
},
"end_location": {
"lat": 17.4395305,
"lng": 78.4483012
},
"html_instructions": "Turn <b>right</b> toward <b>Balkampet Rd</b>",
"maneuver": "turn-right",
"polyline": {
"points": "ecmiBgay}MKdAOdA"
},
"start_location": {
"lat": 17.4393871,
"lng": 78.4489992
},
"travel_mode": "DRIVING"
},
{
"distance": {
"text": "0.2 km",
"value": 230
},
"duration": {
"text": "1 min",
"value": 65
},
"end_location": {
"lat": 17.4374767,
"lng": 78.4482884
},
"html_instructions": "Turn <b>left</b> onto <b>Balkampet Rd</b><div style=\"font-size:0.9em\">Pass by Sai Nirvana (on the left)</div>",
"maneuver": "turn-left",
"polyline": {
"points": "admiB{|x}MfA@b@Ab@AbE@d@?NFNE"
},
"start_location": {
"lat": 17.4395305,
"lng": 78.4483012
},
"travel_mode": "DRIVING"
},
{
"distance": {
"text": "0.5 km",
"value": 462
},
"duration": {
"text": "2 mins",
"value": 98
}.........
答案 0 :(得分:1)
您可以使用以下代码从json中检索所有点。请检查此代码并根据需要更正json参数。
ArrayList<LatLng> listLatLong = new ArrayList<>();
try {
JSONObject json = new JSONObject(result.toString());
JSONArray results = json.getJSONArray("routes");
JSONObject jObjResults = results.getJSONObject(0);
JSONArray jArrLegs = jObjResults.getJSONArray("legs");
JSONObject jObj = jArrLegs.getJSONObject(0);
JSONArray jSteps = jObj.getJSONArray("steps");
for (int i = 0; i < jSteps.length(); i++) {
JSONObject jObjStep = jSteps.getJSONObject(i);
listLatLong.addAll(decodePoly(jObjStep.getJSONObject("polyline").getString("points")));
}
} catch (Exception ignore) {
}
您可以使用
在地图上绘制路线
PolylineOptions rectLine = new PolylineOptions().width(10).color(Color.BLUE);
for (int j = 0; j < listLatLong.size(); j++)
rectLine.add(listLatLong.get(j));
mGoogleMap.addPolyline(rectLine);
decodePoly()方法
public static ArrayList<LatLng> decodePoly(String encoded) {
ArrayList<LatLng> poly = new ArrayList<>();
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;
}
如果您需要我的帮助,请告诉我。