Dessin d'une route entre 2 emplacements de l'API Google Maps Android V2

J'ai été jouer avec l'API Google Maps V2 sur android.
Essayer d'obtenir un chemin entre 2 emplacements et de le faire avec le JSON de l'Analyse.

Je reçois un itinéraire. et le parcours commence sur la façon dont il devrait être. mais alors à un point, il va dans le mauvais sens.

Ma destination finale se termine mal. Et avec quelques autres endroits de mon appli obtient juste terminé.

C'est ce que j'ai fait

Voici mon makeURL méthode

public String makeUrl(){
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin="); //start positie
    urlString.append(Double.toString(source.latitude));
    urlString.append(",");
    urlString.append(Double.toString(source.longitude));
    urlString.append("&destination="); //eind positie
    urlString.append(Double.toString(dest.latitude));
    urlString.append(",");
    urlString.append(Double.toString(dest.longitude));
    urlString.append("&sensor=false&mode=driving");

    return urlString.toString();
}

mon parser JSON

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {
    //TODO Auto-generated constructor stub
}

public String getJSONFromURL(String url){

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        is = httpEntity.getContent();
    } catch(UnsupportedEncodingException e){
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while((line = reader.readLine()) != null){
            sb.append(line + "\n");
            //Log.e("test: ", sb.toString());
        }

        json = sb.toString();
        is.close();
    } catch (Exception e) {
        //TODO Auto-generated catch block
        Log.e("buffer error", "Error converting result " + e.toString());
    }

    return json;
}

Je trace mon Chemin avec cette méthode

public void drawPath(String result){
    try{
        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");
        Log.d("test: ", encodedString);
        List<LatLng> list = decodePoly(encodedString);

        LatLng last = null;
        for (int i = 0; i < list.size()-1; i++) {
            LatLng src = list.get(i);
            LatLng dest = list.get(i+1);
            last = dest;
            Log.d("Last latLng:", last.latitude + ", " + last.longitude );
            Polyline line = googleMap.addPolyline(new PolylineOptions().add( 
                    new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                    .width(2)
                    .color(Color.BLUE));
        }

        Log.d("Last latLng:", last.latitude + ", " + last.longitude );
    }catch (JSONException e){
        e.printStackTrace();
    }
}

Et je décode mon JSON avec

private List<LatLng> decodePoly(String encoded){

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0;
    int length = encoded.length();
    int latitude = 0;
    int longitude = 0;

    while(index < length){
        int b;
        int shift = 0;
        int result = 0;

        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        latitude += destLat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b > 0x20);

        int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        longitude += destLong;

        poly.add(new LatLng((latitude /1E5),(longitude /1E5) ));
    }
    return poly;
}

Puis codés avec un AsyncTask

Merci d'avance.

Merci pour le partage du code, il a aidé. 🙂

OriginalL'auteur Michael | 2013-02-14