Estou usando o seguinte método para calcular a distancia entre 2 cidades usando Google API Matrix:
private function calculaDistancia () {
$this->destino = str_replace(" ","%20",$this->phpUtil->limpaCaracters($this->destino));
$url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=".$this->origem."-".$this->estadoOrigem."&destinations=".$this->destino."-".$this->estadoDestino."&mode=".$this->mode."&language=".$this->language."&sensor=false";
print $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$freteXML = simplexml_load_string($data);
$distancia = $freteXML->row->element->distance->value;
return $distancia;
}
De fato funciona. E a url de pesquisa é:
$url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=".$this->origem."-".$this->estadoOrigem."&destinations=".$this->destino."-".$this->estadoDestino."&mode=".$this->mode."&language=".$this->language;
Bom, o problema é que em uma consulta tive a seguinte url de entrada:
https://maps.googleapis.com/maps/api/distancematrix/xml?origins=Muriae-MG&destinations=Macapá-AP&mode=driving&language=pt-BR&sensor=false
Que retornou o seguinte:
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Muriaé, MG, Brasil</origin_address>
<destination_address>Macapá, AP, Brasil</destination_address>
<row>
<element>
<status>ZERO_RESULTS</status>
</element>
</row>
</DistanceMatrixResponse>
Note que ele identifica as cidades mas não consegue calcular as disâncias.
Algum recurso?
↧