Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Créer un itinéraire

Featured Replies

Posté(e)

Bonjour à tous. Je cherche depuis pas mal de temps un tutoriel pour pouvoir générer un itinéraire entre 2 adresses données, mais sans succès. Si quelqu'un pouvait partager un tuto ou tout autre information sur ce sujet, je lui en serai très reconnaissant.

Merci. :)

Modifié par Cuillère

Posté(e)
  • Auteur

C'est tout à fait ce que fait Google Map. Mais comment intégrer ceci à mon application ? Appeler l'application google map via un intent ?

Posté(e)

J'ai implémenté un calcul d'itinéraire dans une appli. La seule solution que j'ai trouvée est de faire une requête HTTP sur le service goole maps. Il ffaut ensuite parser le résultat qui revient sous forme xml et qui contient une liste de direction.

Exemple, pour aller de Paris à Calais :

http://maps.google.com/maps?f=d&hl=fr&saddr=paris&daddr=calais,france&ie=UTF8&0&om=0

Cette URL ouvre google maps et calcul l'itinéraire.

En ajout output=kml à l'URL, le résultat est retournée dans un format kml (xml)

http://maps.google.com/maps?f=d&hl=fr&saddr=paris&daddr=calais,france&ie=UTF8&0&om=0&output=kml

Extrait de code :


   private List itineraire(GeoPoint from, GeoPoint to) {
       String googleMapURL = buildGoogleMapURL(from, to);
       geometry = new ArrayList();
       placemarks = new ArrayList();
       try
       {
           // connection HTTP
           URL url = new URL(googleMapURL);
           HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
           urlConnection.setRequestMethod("GET");
           urlConnection.setDoOutput(true);
           urlConnection.setDoInput(true);
           urlConnection.connect();

           Log.d("GoogleDirection", "connected");

           // parsing du résultat
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           Document doc = db.parse(urlConnection.getInputStream());

           Log.d("GoogleDirection", "parsed");
           NodeList placemarkCollection = doc.getElementsByTagName("Placemark");
           if (placemarkCollection.getLength() > 0) {
               for(int i = 0; i < placemarkCollection.getLength(); i++) {
                   parsePlacemark(placemarkCollection.item(i));
               }
           }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
   }

   private void parsePlacemark(Node placemark) {
       NodeList child = placemark.getChildNodes();
       String name = null;
       String description = null;
       String address = null;
       GeoPoint point = null;
       for(int i = 0; i < child.getLength(); i++) {
           Node node = child.item(i);
           String nodeName = node.getNodeName();
           if ("name".equals(nodeName)) {
               name = node.getFirstChild().getNodeValue();
           } else if ("description".equals(nodeName)) {
               description = node.getFirstChild().getNodeValue();
           } else if ("address".equals(nodeName)) {
               address = node.getFirstChild().getNodeValue();
           } else if ("Point".equals(nodeName)) {
               String coordinates = node.getFirstChild().getFirstChild().getNodeValue();
               point = parseGeoPoint(coordinates);
           } else if ("GeometryCollection".equals(nodeName)) {
               String path = node.getFirstChild().getFirstChild().getFirstChild().getNodeValue();
               String[] pairs = path.split(" ");
               for(int j = 1; j < pairs.length; j++)
               {
                   geometry.add(parseGeoPoint(pairs[j]));
               }                
           }
       }
       if (point != null) {
           placemarks.add(new Placemark(name, description, address, point));
       }
   }

   private GeoPoint parseGeoPoint(String data) {
       String[] lngLat = data.split(",");
       return new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
   }

   private String buildGoogleMapURL(GeoPoint srcLocation, GeoPoint destLocation) {
       StringBuilder url = new StringBuilder();
       url.append("http://maps.google.com/maps?f=d&hl=");
       url.append(locale);
       url.append("&saddr=");//from
       url.append(Double.toString((double)srcLocation.getLatitudeE6()/1.0E6));
       url.append(",");
       url.append(Double.toString((double)srcLocation.getLongitudeE6()/1.0E6));
       url.append("&daddr=");//to
       url.append(Double.toString((double)destLocation.getLatitudeE6()/1.0E6));
       url.append(",");
       url.append(Double.toString((double)destLocation.getLongitudeE6()/1.0E6));
       url.append("&ie=UTF8&0&om=0&output=kml");
       System.out.println(url);
       return url.toString();
   }

Posté(e)

Lool, j'avais pas capté qu'on était dans la partie programmation, mon commentaire est donc totalement débile!

Pour me faire pardonner, la méthode décrite précedemment est géniale et je t'envoie la méthode qui lange Google Maps:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345");
startActivity(intent);

Cela dépend de ce ue tu veux faire de l'itinéraire. Tu peux même lancer la navigation si tu le désires.

Mea culpa

Posté(e)

Pour lancer GoogleMapNavigation :

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + address)); 
startActivity(i);

Posté(e)
  • Auteur

Seulement l'adresse ? Le point de départ c'est notre position GPS automatiquement ? Et quel format pour l'adresse ? Merci :)

Modifié par Cuillère

Posté(e)

En fait "addesse" correspond à latitude,longitude.

Je n'ai jamais essayé avec une vraie adresse. Dans mon code j'ai des OverlayItem de l'API Maps et j'appel la méthode routableAddress().

Pour obtenir les coordonnées latitude, longitude à partir d'une vraie adresse, tu peux utiliser la classe android.location.GeoCoder et appeler la méthode getFromLocationName(...) qui retourne des objets Adress qui contient les coordonnées.

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.