Nesse tutorial, vou ensinar como fazer uma ferramenta que calcula a distância e o tempo de um local até outro.
O que vai precisar fazer e apenas digitar os dois CEP’s.
cep de origem e o cep destino
*para usar as ferramentas do google, é preciso criar uma KEY
Leia…
https://developers.google.com/maps/documentation/distance-matrix/start
e também.
https://developers.google.com/maps/documentation/distance-matrix/get-api-key
Seus projetos ficam nessa página
https://console.cloud.google.com/project/_/billing/enable
depois de criar sua key coloque aqui.
$link="https://maps.googleapis.com/maps/api/distancematrix/xml?origins=$origem&destinations=$destino&mode=driving&language=pt-BR&sensor=false&key=SUAKEY";
<form name="form1" method="get" action=""> Coloque endereço, CEP ou bairro para saber o tempo(carro) e a distância<br> <br> Origem <input name="origem" type="text" value="<?=$_GET["origem"]?>" size="50"> Destino <input name="destino" type="text" value="<?=$_GET["destino"]?>" size="50"> <input type="submit" value="Calcular"> </form> <? if($_GET["origem"] AND $_GET["destino"]) { header("Content-Type: text/html; charset=UTF-8"); $origem = str_replace(" ", "%20",$_GET["origem"]); $destino = str_replace(" ", "%20",$_GET["destino"]); $link="http://maps.googleapis.com/maps/api/distancematrix/xml?origins=$origem&destinations=$destino&mode=driving&language=pt-BR&sensor=false&key=SUAKEY"; $novolink = curl_init(); curl_setopt($novolink, CURLOPT_URL, "$link"); curl_setopt($novolink, CURLOPT_RETURNTRANSFER, true); curl_setopt ($novolink, CURLOPT_CONNECTTIMEOUT,5); curl_setopt ($novolink, CURLOPT_MAXCONNECTS, 10); curl_setopt($novolink, CURLOPT_TIMEOUT ,5); $output = curl_exec($novolink); curl_close($novolink); $output = str_replace("\n", " ", $output); preg_match_all("/<origin_address>(.*?)<\/origin_address>/",$output, $a); //origem preg_match_all("/<destination_address>(.*?)<\/destination_address>/",$output, $b); //destino preg_match_all("/<duration>(.*?)<\/duration>/",$output, $c0); preg_match_all("/<text>(.*?)<\/text>/",$c0[1][0], $c); //duracao preg_match_all("/<distance>(.*?)<\/distance>/",$output, $d0); preg_match_all("/<text>(.*?)<\/text>/",$d0[1][0], $d); //distancia echo nl2br("<b> Origem: ".utf8_decode($a[1][0])." Destino: ".utf8_decode($b[1][0])." Distância: ".utf8_decode($d[1][0])." Duração: ".utf8_decode($c[1][0])."</b>"); echo '<br><br><iframe id="map" src="https://maps.google.com/maps?saddr='.$a[1][0].'&daddr='.$b[1][0].'&output=embed" width="820" height="520" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></div>'; } ?>
Para testar
acesse o exemplo abaixo:
exemplos/calculardistanciagoogle.php
<< Anterior manual php online e offline
Deixe um comentário