{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 12.8481608,
"lng" : 77.632796
},
"southwest" : {
"lat" : 12.7033123,
"lng" : 77.5885469
}
},
"copyrights" : "Map data ©2015 Google",
"legs" : [
{
"distance" : {
"text" : "20.0 km",
"value" : 19950
},
"duration" : {
"text" : "40 mins",
"value" : 2383
},
"end_address" : "Unnamed Road, Billiganakuppe, Karnataka 562112, India",
"end_location" : {
"lat" : 12.7033123,
"lng" : 77.5885469
},
"start_address" : "Unnamed Road, Bettadasanapura, Bengaluru, Karnataka 560100, India",
"start_location" : {
"lat" : 12.8481608,
"lng" : 77.63232409999999
},
"steps" : [
{
"distance" : {
"text" : "0.4 km",
"value" : 440
},
"duration" : {
"text" : "2 mins",
"value" : 128
},
"end_location" : {
"lat" : 12.844518,
"lng" : 77.6323664
},
"html_instructions" : "Head \u003cb\u003ewest\u003c/b\u003e",
"polyline" : {
"points" : "_llmA_qyxM@`@@L@LBBB@H?N?RADAP?XCLCx@E|@Ez@Gz@Ez@EjEU~@Gb@C"
},
"start_location" : {
"lat" : 12.8481608,
"lng" : 77.63232409999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "30 m",
"value" : 30
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_location" : {
"lat" : 12.8443413,
"lng" : 77.6325708
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "gukmAiqyxMDERWHI"
},
"start_location" : {
"lat" : 12.844518,
"lng" : 77.6323664
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 km",
"value" : 179
},
"duration" : {
"text" : "1 min",
"value" : 48
},
"end_location" : {
"lat" : 12.8428057,
"lng" : 77.6328027
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "ctkmAqryxMFEDCHCHAJAL@|@BpAAZCN?HA`@ELS"
},
"start_location" : {
"lat" : 12.8443413,
"lng" : 77.6325708
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 km",
"value" : 132
},
"duration" : {
"text" : "1 min",
"value" : 57
},
"end_location" : {
"lat" : 12.8416291,
"lng" : 77.63266949999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "qjkmA_tyxM@?DBD?|@BbBFP?D@H?F@F@HB"
},
"start_location" : {
"lat" : 12.8428057,
"lng" : 77.6328027
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 km",
"value" : 114
},
"duration" : {
"text" : "1 min",
"value" : 32
},
"end_location" : {
"lat" : 12.8415124,
"lng" : 77.63163910000002
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "eckmAesyxMBH@D@F?F?H?HAZ?R?B?H?F@L@D@L@HBV@F@B@B"
},
"start_location" : {
"lat" : 12.8416291,
"lng" : 77.63266949999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.0 km",
"value" : 1008
},
"duration" : {
"text" : "3 mins",
"value" : 154
},
"end_location" : {
"lat" : 12.8361958,
"lng" : 77.62500639999999
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "mbkmAwlyxM?@B@@@B@D@t@BhA@P?h@@J@VBJBFDFFJTJf@Lj@Nb@HRhAjClCzF@PFn@?V@TBPHXN^R\\PVLLJFZLd@Ll@Pb@NXRb@b@Vb@Vd@`@|@p@zAVf@DHJVVl@"
},
"start_location" : {
"lat" : 12.8415124,
"lng" : 77.63163910000002
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "5.5 km",
"value" : 5453
},
"duration" : {
"text" : "10 mins",
"value" : 626
},
"end_location" : {
"lat" : 12.7963583,
"lng" : 77.61599439999999
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eBegur - Koppa Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by St Mary's Orthodox Church, Begur (on the right in 2.3 km)\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "gajmAicxxM`@EPAN?VAP@XDd@F~@Nb@Bd@Ap@KnCc@vCc@h@CX?\\@RBPBZFh@L~Ab@z@Tt@RnA`@jBz@tDbBn@TjCdALDf@Jp@Nb@HP@r@HhBJtCBxBFhBJ`BJjER~FXj@Af@CRCHCTKLKHMJQXy@rBsG\\cAb@cAZo@l@y@TYd@e@dAmAx@y@bAiAt@u@t@y@XY^WNGLEJ?J@\\BPBf@J~@RjBT|AN`AFdAFf@?tE?bA?X@RB\\DJBv@F`BBdCBzD@|BF`ADvALjAJ|@Lf@DzFh@pALL@~@Jh@HtAThAL~BLp@@`AHbALdBXn@LTDNDJBJFJFLJr@t@`@f@Zn@f@|@b@p@\\^LLXTfAx@n@j@j@d@\\ZJZZfAVfADLbAzD`@vAnAzDN`@HRZr@\\x@\\v@lAvCNXHPFPN\\tBxExAdD"
},
"start_location" : {
"lat" : 12.8361958,
"lng" : 77.62500639999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "3.1 km",
"value" : 3109
},
"duration" : {
"text" : "7 mins",
"value" : 395
},
"end_location" : {
"lat" : 12.7713362,
"lng" : 77.61348219999999
},
"html_instructions" : "Continue onto \u003cb\u003eKoppa Gate Nisarga Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Anjaneiya ( Hanuman ) Temple Nandanavana (on the right in 3.0 km)\u003c/div\u003e",
"polyline" : {
"points" : "ghbmA}jvxMDJBDDFXNVFx@Xp@T|@\\r@`@h@ZpAt@|BrAhCrAhD|AjClARHRFPBTBbABlCFfB@`@AZGLIRO^c@z@u@d@_@d@YPIbC[TEhBc@t@MVCZEp@AJ?D?D?F?@@B?F@D?BBLF`@R|@ZD@B@@@B@BBBBBJBTBJDLDHBDDFFFTTVTFDFBVFTHJ@JBRBTB^DZ@zDZb@@n@@z@D^B~Ch@dGv@x@Nz@Pv@NTDF?D?D?FAD?BCFAFELIHWLUHGHGJADAF@rANnD^xGt@jDwFPWDIHIFEDCLALAL?T?V@N@RBTDLBJDXLZRNHB?B@LBL@D?B?D?BAh@Kn@Oj@IvAUx@O"
},
"start_location" : {
"lat" : 12.7963583,
"lng" : 77.61599439999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.7 km",
"value" : 675
},
"duration" : {
"text" : "1 min",
"value" : 74
},
"end_location" : {
"lat" : 12.769209,
"lng" : 77.6181972
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "{k}lAg{uxMe@yACIKUEOESCK?G?E?K?OBUBU@GDa@LeBBU^}BBK@IFUFMDGDGLKHGJIJEDCDCFCFCHCHANCJArAIr@GTCLCFADCDABCBCBERa@`@aAHUBIBI@G?EEu@"
},
"start_location" : {
"lat" : 12.7713362,
"lng" : 77.61348219999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "48 m",
"value" : 48
},
"duration" : {
"text" : "1 min",
"value" : 6
},
"end_location" : {
"lat" : 12.7690771,
"lng" : 77.6186106
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "q~|lAwxvxMBA@C@C@C@C@EBG?EBOB["
},
"start_location" : {
"lat" : 12.769209,
"lng" : 77.6181972
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "8.8 km",
"value" : 8762
},
"duration" : {
"text" : "14 mins",
"value" : 852
},
"end_location" : {
"lat" : 12.7033123,
"lng" : 77.5885469
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "w}|lAi{vxMDCDA@?BAD?B?H?H?rDNpETV@bBJV@tBT~@Lx@LJBHDDBFDFDTZZXZPd@Nh@Nd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F`A@`EFbEZ`CJjBHh@F\\Rf@h@Zd@?HIZK\\?RHPRb@HTHNrBbBpAbAHLDRDh@DTDFzBzAp@d@HJHVd@bAPTZRhAZrAHpABp@F^Hn@Vb@Vh@N\\HfB?|@@j@H|APf@?XEp@SrCm@ZG^@h@Hb@RpBp@x@f@VPZ`@r@r@z@h@|BbA~@RvBZx@\\BF?^Id@?h@Ll@\\j@vA`AXPdBz@lB|AfB|Ap@XpBp@zBl@jHlBz@@z@Dl@RjAh@pA`@x@PrBJhBZ\\LNNb@l@j@f@ZL^DlBD`CZjAFxAAPATE^Qb@u@v@aB`@qAR_@POTCb@F`@LPJRTFN@V@\\D`@LPXHt@Pd@XvAzAnF`EJNP`@Zf@nEzBd@h@ZVRNZVx@hAb@d@TLNJ\\Dv@DlADp@DlARxA\\vAR~BR|@@jDC`ELbDLf@H\\JXNfBtAfBhBl@r@h@r@f@v@|@bARLfDxAvA~@xAx@p@RdCXpAXzBZl@PtBx@jBn@"
},
"summary" : "Koppa Gate Nisarga Rd",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
我在调用google maps url时收到上述结果消息...我能够在源和目的地之间绘制路线图。
我如何在源和目的地之间的不同点广告标记。
我已尝试过一些代码
jRoutes = jObject.getJSONArray("routes");
Log.d("in try",jRoutes+"");
Log.d("in try",routes +"");
/** Traversing all routes */
for(int i=0;i<jRoutes.length();i++){
jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
Log.d("jLegs",jLegs+""+"\n" +"");
List path = new ArrayList<HashMap<String, String>>();
Log.d("path",path+""+"\n" +"");
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++){
jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
Log.d("jSteps",jSteps+""+"\n" +"");
/** Traversing all steps */
for(int k=0;k<jSteps.length();k++){
polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
List<LatLng> list = decodePoly(polyline);
position_lat = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("end_location")).get("lat").toString();
position_lon = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("end_location")).get("lng").toString();
list_lat.add(position_lat);
list_lng.add(position_lon);
Log.d("list", list+"");
/** Traversing all points */
for(int l=0;l<list.size();l++){
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) );
hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) );
path.add(hm);
Log.d("latlng", ""+Double.toString(((LatLng)list.get(l)).latitude));
}
}
Log.d("lat", list_lat+"");
Log.d("lon", list_lng+"");
routes.add(path);
}
}
我正在获取纬度和经度列表。我可以将它们转换为latlng并在地图中表示为标记吗?
答案 0 :(得分:1)
您可以参考此代码。
public boolean drawRoute(GoogleMap map, Context c, ArrayList<LatLng> points, boolean withIndications, String language, boolean optimize)
{
mMap = map;
context = c;
lang = language;
if (points.size() == 2)
{
String url = makeURL(points.get(0).latitude, points.get(0).longitude, points.get(1).latitude, points.get(1).longitude, "driving");
new connectAsyncTask(url, withIndications).execute();
return true;
}
else if (points.size() > 2)
{
String url = makeURL(points, "driving", optimize);
new connectAsyncTask(url, withIndications).execute();
return true;
}
return false;
}
private class connectAsyncTask extends AsyncTask<Void, Void, String>
{
private ProgressDialog progressDialog;
String url;
boolean steps;
connectAsyncTask(String urlPass, boolean withSteps)
{
url = urlPass;
steps = withSteps;
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Fetching route, Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params)
{
JSONParser jParser = new JSONParser();
String json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
progressDialog.hide();
if (result != null)
{
drawPath(result, steps);
}
}
}
private void drawPath(String result, boolean withSteps)
{
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);
if(Polyline != null)
{
for (int i = 0; i < Polyline.size(); i++)
{
Polyline line = Polyline.get(i);
line.remove();
}
Polyline.clear();
}
else
{
Polyline = new ArrayList<Polyline>();
}
for (int z = 0; z < list.size() - 1; z++)
{
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)).width(4).color(Color.BLUE)
.geodesic(true));
Polyline.add(line);
}
if(markerPrevious != null)
{
for (int i = 0; i < markerPrevious.size(); i++)
{
Marker marker = markerPrevious.get(i);
marker.remove();
}
markerPrevious.clear();
}
else
{
markerPrevious = new ArrayList<Marker>();
}
if (withSteps)
{
JSONArray arrayLegs = routes.getJSONArray("legs");
JSONObject legs = arrayLegs.getJSONObject(0);
JSONArray stepsArray = legs.getJSONArray("steps");
//put initial point
for (int i = 0; i < stepsArray.length(); i++)
{
Step step = new Step(stepsArray.getJSONObject(i));
Marker marker = mMap.addMarker(new MarkerOptions().position(step.location).title(step.distance).snippet(step.instructions)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
markerPrevious.add(marker);
}
}
}
catch (JSONException e)
{
}
}
我是从这个链接尝试过的 https://github.com/tato469/Android/tree/master/routes%20googleMaps%20v2
答案 1 :(得分:0)
来自Json,您的来源和目的地详细信息为:
Src :
"northeast" : {
"lat" : 12.8481608,
"lng" : 77.632796
},
目的地:
"southwest" : {
"lat" : 12.7033123,
"lng" : 77.5885469
}
},
因此,我们需要在地图上添加两个标记
jRoutes = jObject.getJSONArray("routes");
JSONArray jsonBounds = (JSONObject)jRoutes.get(i)).getJSONArray("bounds");
JSONArray jSrc = ((JSONObject)jsonBounds.get(j)).getJSONArray("northeast");
JSONArray jDest = ((JSONObject)jsonBounds.get(j)).getJSONArray("southwest");
String srcLat = jDest.getString("lat");
String srcLong = jDest.getString("lng");
String destLat = jDest.getString("lat");
String destLong = jDest.getString("lng");
LatLng srcPos = new LatLng(srcLat,srcLong);
LatLng destPos = new LatLng(destLat,destLong);
Marker markerSrc = map.addMarker(new MarkerOptions()
.position(srcPos
.title("Source")
);
Marker markerDest = map.addMarker(new MarkerOptions()
.position(destPos
.title("Destination")
);
map.addMarker(markerSrc);
map.addMarker(markerDest);
更新:
polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
List<LatLng> list = decodePoly(polyline);
position_lat = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("end_location")).get("lat").toString();
position_lon = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("end_location")).get("lng").toString();
list_lat.add(position_lat);
list_lng.add(position_lon);
Log.d("list", list+"");
Marker markerStep = map.addMarker(new MarkerOptions()
.position(new LatLng(position_lat,position_lon))
.title("Destination")
);
答案 2 :(得分:0)
使用此路线类。这是工作代码。
public class Route
{
GoogleMap mMap;
Context context;
String lang;
static String LANGUAGE_SPANISH = "es";
static String LANGUAGE_ENGLISH = "en";
static String LANGUAGE_FRENCH = "fr";
static String LANGUAGE_GERMAN = "de";
static String LANGUAGE_CHINESE_SIMPLIFIED = "zh-CN";
static String LANGUAGE_CHINESE_TRADITIONAL = "zh-TW";
static String TRANSPORT_DRIVING = "driving";
static String TRANSPORT_WALKING = "walking";
static String TRANSPORT_BIKE = "bicycling";
static String TRANSPORT_TRANSIT = "transit";
static ArrayList<Polyline> Polyline ;
static ArrayList<Marker> markerPrevious;
public boolean drawRoute(GoogleMap map, Context c, ArrayList<LatLng> points, boolean withIndications, String language, boolean optimize)
{
mMap = map;
context = c;
lang = language;
if (points.size() == 2)
{
String url = makeURL(points.get(0).latitude, points.get(0).longitude, points.get(1).latitude, points.get(1).longitude, "driving");
new connectAsyncTask(url, withIndications).execute();
return true;
}
else if (points.size() > 2)
{
String url = makeURL(points, "driving", optimize);
new connectAsyncTask(url, withIndications).execute();
return true;
}
return false;
}
public boolean drawRoute(GoogleMap map, Context c, ArrayList<LatLng> points, String language, boolean optimize)
{
mMap = map;
context = c;
lang = language;
if (points.size() == 2)
{
String url = makeURL(points.get(0).latitude, points.get(0).longitude, points.get(1).latitude, points.get(1).longitude, "driving");
new connectAsyncTask(url, false).execute();
return true;
}
else if (points.size() > 2)
{
String url = makeURL(points, "driving", optimize);
new connectAsyncTask(url, false).execute();
return true;
}
return false;
}
public boolean drawRoute(GoogleMap map, Context c, ArrayList<LatLng> points, String mode, boolean withIndications, String language,
boolean optimize)
{
mMap = map;
context = c;
lang = language;
if (points.size() == 2)
{
String url = makeURL(points.get(0).latitude, points.get(0).longitude, points.get(1).latitude, points.get(1).longitude, mode);
new connectAsyncTask(url, withIndications).execute();
return true;
}
else if (points.size() > 2)
{
String url = makeURL(points, mode, optimize);
new connectAsyncTask(url, withIndications).execute();
return true;
}
return false;
}
//
public void drawRoute(GoogleMap map, Context c, LatLng source, LatLng dest, boolean withIndications, String language)
{
mMap = map;
context = c;
String url = makeURL(source.latitude, source.longitude, dest.latitude, dest.longitude, "driving");
new connectAsyncTask(url, withIndications).execute();
lang = language;
}
public void drawRoute(GoogleMap map, Context c, LatLng source, LatLng dest, String language)
{
mMap = map;
context = c;
String url = makeURL(source.latitude, source.longitude, dest.latitude, dest.longitude, "driving");
new connectAsyncTask(url, false).execute();
lang = language;
}
public void drawRoute(GoogleMap map, Context c, LatLng source, LatLng dest, String mode, boolean withIndications, String language)
{
mMap = map;
context = c;
String url = makeURL(source.latitude, source.longitude, dest.latitude, dest.longitude, mode);
new connectAsyncTask(url, withIndications).execute();
lang = language;
}
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();
}
private String makeURL(double sourcelat, double sourcelog, double destlat, double destlog, String mode)
{
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(Double.toString(sourcelat));
urlString.append(",");
urlString.append(Double.toString(sourcelog));
urlString.append("&destination=");// to
urlString.append(Double.toString(destlat));
urlString.append(",");
urlString.append(Double.toString(destlog));
urlString.append("&sensor=false&mode=" + mode + "&alternatives=true&language=" + lang);
return urlString.toString();
}
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;
}
private class connectAsyncTask extends AsyncTask<Void, Void, String>
{
private ProgressDialog progressDialog;
String url;
boolean steps;
connectAsyncTask(String urlPass, boolean withSteps)
{
url = urlPass;
steps = withSteps;
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Fetching route, Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params)
{
JSONParser jParser = new JSONParser();
String json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
progressDialog.hide();
if (result != null)
{
drawPath(result, steps);
}
}
}
private void drawPath(String result, boolean withSteps)
{
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);
if(Polyline != null)
{
for (int i = 0; i < Polyline.size(); i++)
{
Polyline line = Polyline.get(i);
line.remove();
}
Polyline.clear();
}
else
{
Polyline = new ArrayList<Polyline>();
}
for (int z = 0; z < list.size() - 1; z++)
{
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)).width(4).color(Color.BLUE)
.geodesic(true));
Polyline.add(line);
}
if(markerPrevious != null)
{
for (int i = 0; i < markerPrevious.size(); i++)
{
Marker marker = markerPrevious.get(i);
marker.remove();
}
markerPrevious.clear();
}
else
{
markerPrevious = new ArrayList<Marker>();
}
if (withSteps)
{
JSONArray arrayLegs = routes.getJSONArray("legs");
JSONObject legs = arrayLegs.getJSONObject(0);
JSONArray stepsArray = legs.getJSONArray("steps");
//put initial point
for (int i = 0; i < stepsArray.length(); i++)
{
Step step = new Step(stepsArray.getJSONObject(i));
Marker marker = mMap.addMarker(new MarkerOptions().position(step.location).title(step.distance).snippet(step.instructions)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
markerPrevious.add(marker);
}
}
}
catch (JSONException e)
{
}
}
/**
* Class that represent every step of the directions. It store distance, location and instructions
*/
private class Step
{
public String distance;
public LatLng location;
public String instructions;
Step(JSONObject stepJSON)
{
JSONObject startLocation;
try
{
distance = stepJSON.getJSONObject("distance").getString("text");
startLocation = stepJSON.getJSONObject("start_location");
location = new LatLng(startLocation.getDouble("lat"), startLocation.getDouble("lng"));
try
{
instructions = URLDecoder.decode(Html.fromHtml(stepJSON.getString("html_instructions")).toString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
;
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
致电:
ArrayList<LatLng> allatlng = new ArrayList<LatLng>();
LatLng latlng = new LatLng(Double.parseDouble(strDestlat), Double.parseDouble(strDestlng));
allatlng.add(latlng);
LatLng latlng1 = new LatLng(Double.parseDouble(strCurrentLat), Double.parseDouble(strCurrentLong));
allatlng.add(latlng1);
Route route = new Route();
route.drawRoute(googleMap, Home.this, allatlng, true, Route.LANGUAGE_ENGLISH, true);