Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

android - map v2 drawed polylines are not exactly on the road

i fetched points from this url just for test :

https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey

i run this AsyncTask class in order to get and draw directions, this class in used in Map class extending AppCompatActivity:

private class direction extends AsyncTask<String, Void, String> {
    private Context mContext;
    List<LatLng> pontos;
    ProgressDialog dialog;
    double currentLatitude, currentLongitude,destinationLatitude,destinationLongitude;
    String walkDistance;

    public direction (Context context,LatLng point){
        mContext = context;
        destinationLatitude = point.latitude;
        destinationLongitude = point.longitude;
    }


    @Override
    protected String doInBackground(String... params) {
        JSONObject obj;


        String response = HttpRequest.get("https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey").body();


        try {
            System.out.println("Response content 1 was " + response);

            String jsonOutput = response.toString();

            JSONObject jsonObject = new JSONObject(jsonOutput);

            // routesArray contains ALL routes
            JSONArray routesArray = jsonObject.getJSONArray("routes");
            // Grab the first route
            JSONObject route = routesArray.getJSONObject(0);

            JSONArray legs = route.getJSONArray("legs");
            JSONObject firtsLegs = legs.getJSONObject(0);

            JSONObject distance = firtsLegs.getJSONObject("distance");


            System.out.println("Response test was : " + distance.getString("text"));

            walkDistance = distance.getString("text");

            //Toast.makeText(getApplicationContext(), "????? ??? ?? ????: " + distance.getString("text"), Toast.LENGTH_LONG).show();


            JSONObject poly = route.getJSONObject("overview_polyline");
            String polyline = poly.getString("points");
            pontos = decodePoly(polyline);


            return response;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";

    }

    @Override
    protected void onPostExecute(String result) {

        Toast.makeText(getApplicationContext(), "distance : "+walkDistance, Toast.LENGTH_LONG).show();

        /*System.out.println("Response content 2 was " + result);
        Polygon polygon = mapFragment.addPolygon(new PolygonOptions()
        .add(new LatLng(35.7513974, 51.4350038),
             new LatLng(35.7713974, 51.4350038),
             new LatLng(35.7913974, 51.4350038),
             new LatLng(35.7913974, 51.5350038))
        .strokeWidth(5)
        .strokeColor(Color.BLUE));*/


        for (int i = 0; i < pontos.size() - 1; i++) {
            LatLng src = pontos.get(i);
            LatLng dest = pontos.get(i + 1);
            try{
                //here is where it will draw the polyline in your map
                /*Polyline line = googleMap.addPolyline(new PolylineOptions()
                        .add(new LatLng(src.latitude, src.longitude),
                                new LatLng(dest.latitude,                dest.longitude))
                        .width(10).color(Color.BLUE).geodesic(false));

                */

                Polygon line = googleMap.addPolygon(new PolygonOptions()
                        .add(new LatLng(src.latitude, src.longitude),
                                new LatLng(dest.latitude,                dest.longitude))
                        .strokeColor(Color.BLUE).geodesic(true));



            }catch(NullPointerException e){
                Log.e("Error", "NullPointerException onPostExecute: " + e.toString());
            }catch (Exception e2) {
                Log.e("Error", "Exception onPostExecute: " + e2.toString());
            }

        }

        dialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Map.this);
        dialog.setMessage("?? ??? ???? ???? ???? ??? ?????? ???? ??? ????.");
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.show();


        /*gps = new GPSTracker(Map.this);

        // Check if GPS enabled
        if(gps.canGetLocation()) {

            currentLatitude = gps.getLatitude();
            currentLongitude = gps.getLongitude();

            // 
 is for new line
            Toast.makeText(getApplicationContext(), "Your Location is - 
Lat: " + currentLatitude + "
Long: " + currentLongitude, Toast.LENGTH_LONG).show();
        } else {
            // Can't get location.
            // GPS or network is not enabled.
            // Ask user to enable GPS/network in settings.
            gps.showSettingsAlert();
        }*/





    }

    @Override
    protected void onProgressUpdate(Void... values) {}
}

but lines are not on the road and it looks like bellow image :

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using the overview_polyline from the returned routes object to draw your path. As the documentation says (emphasis mine)

overview_polyline contains a single points object that holds an encoded polyline representation of the route. This polyline is an approximate (smoothed) path of the resulting directions.

You will get a much better approximation if you draw the polyline object of each step that you receive in the legs array.

From your example (the red line represents the overview_polyline and the blue line represents the polyline object of the first step received):

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...