1

Google Geolocation 不接受来自数据库的字符串

当我在字符串中手动输入地址时,它将起作用。

$city ="Karlstraße 33
36037 Fulda";
print_r (locate($city));

但是 city 是数据库中的一个字符串,它不起作用。

$city =$obj->adresse;
    echo $city."</br>";
    $city = locate($city);

curl_exec($ch):
status" : "INVALID_REQUEST"

我使用这些功能:

   function locate($string){
   $string = str_replace (" ","+",urlencode($string));//urlencode($string)
   $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $details_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // $response = json_decode(curl_exec($ch), true);
  echo curl_exec($ch);
//print_r($response);
   // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
   if ($response['status'] != 'OK') {
    return null;
   }
   //print_r($details_url);
   //print_r($response);
   $geometry = $response['results'][0]['geometry'];

    $longitude = $geometry['location']['lat'];
    $latitude = $geometry['location']['lng'];

    $array = array(
        'latitude' => $geometry['location']['lng'],
        'longitude' => $geometry['location']['lat'],
        'location_type' => $geometry['location_type'],
    );
    return $array;

}
4

0 回答 0