Add Country to Distance Matrix API

Asked

Viewed 169 times

-1

I’m using a function to return from the google Matrix api, the distance between two cities.

The problem is that when cities have names similar to those of North American cities, the calculation of the distance gives error.

Is there a way to inform the API that the country is Brazil?

That is the function:

  private function calculaDistancia () {

    $this->destino =  str_replace(" ","%20",$this->phpUtil->limpaCaracters($this->destino));

    $url = "http://maps.googleapis.com/maps/api/distancematrix/xml?Key=".$this->key."&origins=''".$this->origem."''|&destinations=''".$this->destino."''|&mode=''".$this->mode."''|&language=''".$this->language."''|&sensor=false
";

    $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;

  }

here is the attempt with the city California-PR

http://maps.googleapis.com/maps/api/distancematrix/xml?origins=%27%27Muriae%27%27|&Destinations=%27%27Calif%C3%B3rnia%27%27|&mode=%27%27CAR%27%27|&language=%27%27pt-br%27%27%27|&sensor=false

Note: I have tried

Region=br parents=br

Nothing works out.

1 answer

0

I think I got.

Just add to the city, your state.

$url = "http://maps.googleapis.com/maps/api/distancematrix/xml?Key=".$this->key."&origins=".$this->origem."-".$this->estadoOrigem."&destinations=".$this->destino."-".$this->estadoDestino."&mode=".$this->mode."&language=".$this->language."&sensor=false";   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.