Search code examples
androidgoogle-maps-api-2polyline

Downloading polyline information


I'm trying to download and show polyline.

I have this async task setup:

private class taskGetPolyline extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;
    StringBuilder urlString;
    String response;
    int i;

    private taskGetPolyline(int i) {
        this.i = i; //passes in id of closest marker
    }

    @Override protected void onPreExecute() { 
        super.onPreExecute();

        dialog = new ProgressDialog(ContentActivity.this);
        dialog.setTitle(getResources().getText(
                R.string.progress_download_route));
        dialog.setMessage(getResources().getText(
                R.string.progress_please_wait)); dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false); dialog.show();

        urlString = new StringBuilder(); urlString
                .append("http://maps.googleapis.com/maps/api/directions/json");
        urlString.append("?origin=");
        urlString.append(LOCATION_CURRENT.getLatitude()); urlString.append(",");
        urlString.append(LOCATION_CURRENT.getLongitude());
        urlString.append("&destination="); 

        urlString.append(arrayLocation[i].latitude);
        urlString.append(","); 
        urlString.append(arrayLocation[i].longitude);
        urlString.append("&sensor=false");

        Log.i("DEBUG","link: " + urlString.toString());

  }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            HttpClient Client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(urlString.toString());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            response = Client.execute(httpGet, responseHandler);

            Log.i("DEBUG", "response = " + response);
        } catch (Exception e) {
            Log.i("Exception", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        JSONObject jsonResponse;
        try {
            jsonResponse = new JSONObject(response);
            JSONObject jsonOverviewPolylines = jsonResponse
                    .getJSONObject("overview_polyline");

            PolylineOptions polylineOptions = new PolylineOptions()
                    .addAll(decodePolyLine(jsonOverviewPolylines.toString()));
            currentPolyline = map.addPolyline(polylineOptions);



        } catch (JSONException e) {

            e.printStackTrace();
        }
        dialog.dismiss();
    }

}

but when i run this code I get warning in logcat:

10-31 14:48:43.744: W/System.err(10724): org.json.JSONException: No value for overview_polyline

log displayes whole link, that's fine. here's example: http://maps.googleapis.com/maps/api/directions/json?origin=46.14928015,16.00907806&destination=45.831534,16.02944&sensor=false

response is not displayed till the end in the log:

10-31 14:55:35.744: I/DEBUG(11671): response = {
10-31 14:55:35.744: I/DEBUG(11671):    "routes" : [
10-31 14:55:35.744: I/DEBUG(11671):       {
10-31 14:55:35.744: I/DEBUG(11671):          "bounds" : {
10-31 14:55:35.744: I/DEBUG(11671):             "northeast" : {
10-31 14:55:35.744: I/DEBUG(11671):                "lat" : 46.1724292,
10-31 14:55:35.744: I/DEBUG(11671):                "lng" : 16.0385437
10-31 14:55:35.744: I/DEBUG(11671):             },
10-31 14:55:35.744: I/DEBUG(11671):             "southwest" : {
10-31 14:55:35.744: I/DEBUG(11671):                "lat" : 45.7876853,
10-31 14:55:35.744: I/DEBUG(11671):                "lng" : 15.8190462
10-31 14:55:35.744: I/DEBUG(11671):             }
10-31 14:55:35.744: I/DEBUG(11671):          },
...
10-31 14:55:35.744: I/DEBUG(11671):                      "html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e toward \u003cb\u003eVelika Petrovagorska\u003c/b\u003e",
10-31 14:55:35.744: I/DEBUG(11671):                      "maneuver" : "turn-slight-right",
10-31 14:55:35.744: I/DEBUG(11671):                      "polyline" : {
10-31 14:55:35.744: I/DEBUG(11671):                         "points" : "yxtxGixu`BSMQGy@QWKWQOOMKKOGMCO?QFOJUL[FQ@OB_@Bs@@k@?e@E_AI{@IiAEYEWCQCY?Y?]@m@"
10-31 14:55:35.744: I/DEBUG(11671):                      },
10-31 14:55:35.744: I/DEBUG(11671):                      "start_location" : {
10-31 14:55:35.744: I/DEBUG(11671):                         "lat" : 46.1506939,
10-31 14:55:35.744: I/DEBUG(11671):                         "lng" : 16.0091667
10-31 14:55:35.744: I/DEBUG(11671):                      },
10-31 14:55:35.744: I/DEBUG(11671):                      "travel_mode" : "DRIVING"
10-31 14:55:35.744: I/DEBUG(11671):                   },
10-31 14:55:35.744: I/DEBUG(11671):                   {
10-31 14:55:35.744: I/DEBUG(11671):                      "distance" : {
10-31 14:55:35.744: I/DEBUG(11671):                         "text" :

I'm missing this part from response:

     "overview_polyline" : {
        "points" : "iptxGcxu`Bu@r@wAe@uCa@mDqBj@eD_@}JGcBMu@}@_@mCc@m@B}GpBkFvBuOxDkE~AgGtDgFb@qFe@}EUkC^qA~AeAb@aBi@aC~AeFrAsDzBo@u@uCWuFfAmEfB}AtB?tAnKbPdIbOEh@a@h@}An@g@`C~@bDUrEAhAdEbCtCvBb@xA?nIv@bAzFbAdCjCrEdMt@jNmAlDoBbFO`Gf@vFrBpIXtBbBjCxEzJ|@xC|B|IjBdBjAbEjBxAxAW`BOvB|BvDhEpHlC|AnCxAhEr@v@?j@sExBcApANr@f@hAFlAzApFCpFiBv@_AzAbApEhAlC`AiB`AoAvC^nBvDxAv@jE}Cx@WnA~BfB]d@PQfCsA~Hw@fJ\\vLdCzEv@~GdBxBxBdGdAhEjA~Ab@xF\\`Bm@hAaAjAg@lGAzMtA`IlGdHzJtFlF`B|Mw@dGvExMbMnATx@c@lEmFrBFtBbAxBJ~BaApEkCjMz@lI|@hCQbF`B~IAdHzBpFlDrIhBvBrErEvj@nBb[hAfGHpGWjO@pHbBnChCtIhBdKdFdObCxBrCjDhAzFbBnAtHDlSyAlFl@`HnBzG~@bDMfDm@tC?tCGvA^rFeAlFv@|DTbCxB?NRPNWAOp@wBnD{Kl@NhBGbNuJlEc@nBlApBxDrBtFbBd@nAcCDyDfAoEpNgLfNmJbX}M~JuD~VwGtQ{Cda@oCb_@v@nQbAnK[fKkCns@e`@~NaF~MqAjNb@vLrClK~FxHjHxj@l~@rRb`@lMzZbw@rtBdSph@~In\\rFjP|GfMzHvJzIjH|GdDzzAdf@niAr^|I`EzFxCrMhJjSnSbHhIvSvTtGxG|MzLjQlLzUzK`KfD`XlFnh@nFvVjBrSbCzYdHta@pNxT|IbPfExVdDdXFfXgChTaFpr@cXzMaEp`@_HvPw@pPiBlIaCnSgF|Q_B|LHvMjAjLjCdS~Hvb@pTpZnObSpIdTnFxIxApVfBbZK~[oD|WmHjWuL|[wUhW_Xrj@cm@n_@{_@fO{JlSaIdFwCvDcDbCwB|B~@DlEw@fAoABuAwAmAiCeJ}SyJwUaJiWuFwVwC}SeBcUs@u_@Zwk@b@y}@`Bc_ApGkdB~DodAtA}_Aw@}b@c@y^b@al@Fmi@uA_t@_D_cBcByy@BwGt@U|@zCeB|Ck@`@kOfL}Y`QkPlHwBv@{I{h@sCwLkCiRoWmyA_Jey@eK_y@WqCaAPuGhBqVnFyDh@cBj@R|Ax@vFhB~M{@JkLh@oLp@}K~CuF~ByAh@NdA"
     },
     "summary" : "E59",
     "warnings" : [],
     "waypoint_order" : []
  }
   ],
"status" : "OK"
}

Anyone have any idea why i can't get whole response, and then parse only "overview_polyline" tag?


Solution

  • You are not reading it properly from the jsonResponse object. You try to read from the base Json but it's actually inside one of the Jsons from "routes" tag. Your code for picking the first route from response would be something like this:

    JsonArray routes = jsonResponse.optJsonArray("routes");
    if (routes != null && route.length() > 0) {
        JsonObject firstRoute = route.optJsonObject(1);
        if (firstRoute != null) {
            JsonArray waypoints = firstRoute.optJsonArray("waypoint_order");
            JsonObject overviewPolilyne = firstRoute.optJsonObject("overview_polyline");
        }
    }