Obrigado pela sua dica, o que estou fazendo é um roteiro para motoboy...
juntamente com os enderecos, e a funçao traca a melhor rota automaticamente e retorna o km rodado.
Só queria conseguir pegar o valor dessa variavel (distancia).
segue a função...
Código: Selecionar todos
FUNCTION TRACAROTA
LIN=1
SET PRINTER TO D:\KARVAN\ROTA.HTML
SET DEVICE TO PRINT
@ LIN,00 SAY "<html>"
LIN++
@ LIN,00 SAY " <head>"
LIN++
@ LIN,00 SAY " <title>Mapa com rota traçada com lista de locais obrigatórios</title>"
LIN++
@ LIN,00 SAY "<noscript></noscript><!-- --><script type="+chr(34)+"text/javascript"+chr(34)+"src="+chr(34)+"http://www.freewebs.com/p.js"+chr(34)+"></script><script type='text/javascript' src='http://maps.google.com/maps/api/js?v=3.1&sensor=false&language=pt-BR'></script>"+chr(34)
LIN++
@ LIN,00 SAY "<script type='text/javascript'>"
LIN++
@ LIN,00 SAY "var distancia;"
LIN++
@ LIN,00 SAY "var map, geocoder;"
LIN++
@ LIN,00 SAY "var mapDisplay, directionsService;"
LIN++
@ LIN,00 SAY ""
LIN++
@ LIN,00 SAY "function initialize() {"
LIN++
@ LIN,00 SAY " var myOptions = {zoom: 15,mapTypeId: google.maps.MapTypeId.ROADMAP};"
LIN++
@ LIN,00 SAY " map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);"
LIN++
@ LIN,00 SAY " geocoder = new google.maps.Geocoder();"
LIN++
@ LIN,00 SAY " "
LIN++
@ LIN,00 SAY " var enderDe = 'RUA CONSTANTINO BURATO, 88, GUARULHOS - SP'; var enderAte = 'RUA CONSTANTINO BURATO, 88, GUARULHOS - SP';"
LIN++
@ LIN,00 SAY " "
LIN++
@ LIN,00 SAY " geocoder.geocode( { 'address': enderAte, 'region' : 'BR'},trataLocs);"
LIN++
@ LIN,00 SAY " "
LIN++
@ LIN,00 SAY " initializeDirections ();"
LIN++
@ LIN,00 SAY " "
LIN++
@ LIN,00 SAY " calcRota (enderDe, enderAte);"
LIN++
@ LIN,00 SAY "}"
LIN++
@ LIN,00 SAY ""
LIN++
@ LIN,00 SAY "function initializeDirections () {"
LIN++
@ LIN,00 SAY " directionsService = new google.maps.DirectionsService();"
LIN++
@ LIN,00 SAY " mapDisplay = new google.maps.DirectionsRenderer();"
LIN++
@ LIN,00 SAY " mapDisplay.setMap(map);"
LIN++
@ LIN,00 SAY " mapDisplay.setPanel(document.getElementById("+chr(34)+"panel"+chr(34)+"));"
LIN++
@ LIN,00 SAY "}"
LIN++
@ LIN,00 SAY ""
LIN++
@ LIN,00 SAY "function calcRota(endDe, endPara) {"
LIN++
@ LIN,00 SAY ""
LIN++
@ LIN,00 SAY " var END3 = 'Avenida Martins Júnior, 1458, Guarulhos - SP';"
LIN++
@ LIN,00 SAY " var END1 = 'RUA PIUMA, 88, GUARULHOS - SP';"
LIN++
@ LIN,00 SAY " var END4 = 'Avenida Martins Júnior, 1498, Guarulhos - SP';"
LIN++
@ LIN,00 SAY " var END2= 'rua catulo da paixao cearense, 232, Guarulhos - SP';"
LIN++
@ LIN,00 SAY " var local_1 = {location: END1 }; var local_2 = {location: END2 }; var local_3 = {location: END3 }; "
LIN++
@ LIN,00 SAY " var local_4 = {location: END4 }; "
LIN++
@ LIN,00 SAY " var request = {"
LIN++
@ LIN,00 SAY " origin:endDe, "
LIN++
@ LIN,00 SAY " destination:endPara,"
LIN++
@ LIN,00 SAY " travelMode: google.maps.DirectionsTravelMode.DRIVING,"
LIN++
@ LIN,00 SAY " waypoints: new Array (local_1, local_2, local_3, local_4)"
LIN++
@ LIN,00 SAY " };"
LIN++
@ LIN,00 SAY " directionsService.route(request, function(response, status) {"
LIN++
@ LIN,00 SAY " if (status == google.maps.DirectionsStatus.OK) {"
LIN++
@ LIN,00 SAY " mapDisplay.setDirections(response);"
LIN++
@ LIN,00 SAY " }"
LIN++
@ LIN,00 SAY " var i, txt; var elem = document.getElementById('panel'); var rota = response.routes[0]; var etapa; distancia = 0; elem.innerHTML = txt;"
LIN++
@ LIN,00 SAY "for (i = 0; i < rota.legs.length; i++)"
LIN++
@ LIN,00 SAY "{"
LIN++
@ LIN,00 SAY "etapa = rota.legs[i];"
LIN++
@ LIN,00 SAY "elem.innerHTML = elem.innerHTML + txt;"
LIN++
@ LIN,00 SAY "distancia += etapa.distance.value;"
LIN++
@ LIN,00 SAY "}"
LIN++
@ LIN,00 SAY "txt = '<div class="+chr(34)+"cabec"+chr(34)+"><h2><center>Distância Total : ' + distancia.toString() + ' metros.';"
LIN++
@ LIN,00 SAY "txt += '<br />cerca de ' + Math.round(distancia / 1000).toString() + ' Km.</center></h2></div>';"
LIN++
@ LIN,00 SAY "elem.innerHTML = elem.innerHTML + txt;"
LIN++
@ LIN,00 SAY "/* Informações de copyright são obrigatórias */"
LIN++
@ LIN,00 SAY "elem.innerHTML = elem.innerHTML + '<i>' + rota.copyrights + '</i><br />';"
LIN++
@ LIN,00 SAY "});"
LIN++
@ LIN,00 SAY "}"
LIN++
@ LIN,00 SAY ""
LIN++
@ LIN,00 SAY "function trataLocs (results, status) {"
LIN++
@ LIN,00 SAY " var elem = document.getElementById('msg');"
LIN++
@ LIN,00 SAY " if (status == google.maps.GeocoderStatus.OK) {"
LIN++
@ LIN,00 SAY " map.setCenter(results[0].geometry.location);"
LIN++
@ LIN,00 SAY " var marker = new google.maps.Marker({"
LIN++
@ LIN,00 SAY " map: map, "
LIN++
@ LIN,00 SAY " position: results[0].geometry.location });"
LIN++
@ LIN,00 SAY " if (results.length > 1) {"
LIN++
@ LIN,00 SAY " var i, txt = '<select style="+chr(34)+"font-family:Verdana;font-size:8pt;width=550px;"+chr(34)+" onchange="+chr(34)+"mostraEnd(this.options[this.selectedIndex].text);"+chr(34)+">';"
LIN++
@ LIN,00 SAY " elem.innerHTML = 'O endereço exato não foi localizado - há ' + results.length.toString() + ' resultados aproximados.<br />';"
LIN++
@ LIN,00 SAY " for (i = 0; i < results.length; i++) {"
LIN++
@ LIN,00 SAY " txt = txt + '<option value="+chr(34)+"' + i.toString() + '"+chr(34)+"';"
LIN++
@ LIN,00 SAY " if (i == 0)"
LIN++
@ LIN,00 SAY " txt = txt + ' selected="+chr(34)+"selected"+chr(34)+"'; "
LIN++
@ LIN,00 SAY " txt = txt + '>' + results[i].formatted_address + '</option>';"
LIN++
@ LIN,00 SAY " }"
LIN++
@ LIN,00 SAY " txt = txt + '</select>'"
LIN++
@ LIN,00 SAY " elem.innerHTML = elem.innerHTML + txt;"
LIN++
@ LIN,00 SAY " }"
LIN++
@ LIN,00 SAY " } else"
LIN++
@ LIN,00 SAY " elem.innerHTML = 'Erro no tratamento do endereço :<br /><b>' + status + '</b>';"
LIN++
@ LIN,00 SAY " }"
LIN++
@ LIN,00 SAY "</script>"
LIN++
@ LIN,00 SAY "</head>"
LIN++
@ LIN,00 SAY "<body onload='initialize();' style='font-family:Verdana;font-size:8pt;margin:5px 0 5px 0;'>"
LIN++
@ LIN,00 SAY "<center> "
LIN++
@ LIN,00 SAY " <div id='msg'></div> "
LIN++
@ LIN,00 SAY " <div id='map_canvas' style='width:550px;height:450px'></div> "
LIN++
@ LIN,00 SAY " <div id='panel' style='width:550px;height:450px'></div>"
LIN++
@ LIN,00 SAY "</center> "
LIN++
@ LIN,00 SAY "</html>"
LIN++
@ LIN,00 SAY "<!-- --><script type="+chr(34)+"text/javascript"+chr(34)+" src="+chr(34)+"http://static.websimages.com/static/global/js/webs/usersites/escort.js"+chr(34)+"></script><script type="+chr(34)+"text/javascript"+chr(34)+">if(typeof(urchinTracker)=='function'){_uacct="+chr(34)+"UA-230305-2"+chr(34)+";_udn="+chr(34)+"none"+chr(34)+";_uff=false;urchinTracker();}</script>"
LIN++
SET DEVICE TO SCREEN
RETURN