Fonctions de calcul de temps de trajet et de distance
Page modifiée le 15/09/2012
Function get_km(place_a, place_b)
my_xml_path = "http://maps.google.fr/maps?saddr=" & place_a & "&daddr=" & place_b & "&ie=utf-8&v=2.1&cv=4.0.2744&hl=fr&output=kml"
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.Load (my_xml_path)
Set nodelist = xmlDoc.getElementsByTagName("description")
my_raw_string = nodelist.Item(nodelist.Length - 1).firstchild.nodevalue
get_km = Monextract(my_raw_string, ": ", "&")
End Function
Function get_driving_time(place_a, place_b)
my_xml_path = "http://maps.google.fr/maps?saddr=" & place_a & "&daddr=" & place_b & "&ie=utf-8&v=2.1&cv=4.0.2744&hl=fr&output=kml"
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.Load (my_xml_path)
Set nodelist = xmlDoc.getElementsByTagName("description")
my_raw_string = nodelist.Item(nodelist.Length - 1).firstchild.nodevalue
get_driving_time = Monextract(my_raw_string, "environ ", ")")
get_driving_time = Replace(get_driving_time, "minutes", "")
If InStr(get_driving_time, "heure") = 0 Then get_driving_time = "00:" & get_driving_time
get_driving_time = Replace(get_driving_time, "heures", ":")
get_driving_time = Replace(get_driving_time, "heure", ":")
get_driving_time = get_driving_time & ":00"
get_driving_time = Replace(get_driving_time, " ", "")
get_driving_time = TimeValue(get_driving_time)
End Function
Function Monextract(machaine, debut, fin)
PosH1 = InStr(1, machaine, debut)
PosH2 = InStr(1, machaine, fin)
long_first = Len(debut)
Leng = PosH2 - PosH1 - long_first
Monextract = Mid(machaine, PosH1 + long_first, Leng)
End Function
Page modifiée le 15/09/2012
- Connectez-vous ou inscrivez-vous pour publier un commentaire
- 981 lectures


Commentaires
Bluffant !
on sam, 03/20/2010 - 11:47
"Bluffant !
Mais avare de commentaires, on ne peut pas tout avoir.
Mettre dans une cellule :
=get_km(paris";"lyon")
nb : à condition d'habiter Paris et de se rendre à Lyon, bien sur. Sinon ça perd de son intérêt... 8-)
edit : et aussi =get_driving_time("paris";"lyon") puis format de cell en hh:mm"
Petit Problème avec Get_km
on sam, 08/07/2010 - 17:34
Petite découverte pour moi de la manipulation de MSKML2.DOMDocument
j'ai testé la fonction Get_km, qui, bizarrement fonctionne bien tant que l'on indique deux villes dont la distance est inférieure à 1000 km
J'ai fait le test sur ?Get_mk(Brest","Strasbourg") en mode pas à pas et la variable my_raw_string contient le texte suivant : (je n'ai pris que le début)
Distance: 1 069 km (environ 10 heures 16 minutes)
Bizarre isn't it !!!
j'ai fait plusieurs test, l'erreur semble systématique dès qu'on dépasse 1000 km
Michel Allaire (de la java de la Varenne)"