Google Maps API无法在实际网站

时间:2015-05-13 00:11:05

标签: php google-maps

function getlatlang($location,$region)  
{  
    $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&sensor=false$region='. urlencode($region));  

    $output= json_decode($geocode);  

    return $output->results[0]->geometry->location;  
}

这是我在本地服务器上进行开发时一直使用的,现在我已经通过我的webhost使网站生效了,这不再返回位置。

不确定它有什么问题?

1 个答案:

答案 0 :(得分:0)

function getlatlang($location,$region)  
{  
    //$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&sensor=false&region='. urlencode($region));

    $url = 'http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&sensor=false&region='. urlencode($region);
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $geocode = curl_exec($ch);
    if (curl_errno($ch)) {
      echo curl_error($ch);
      echo "\n<br />";
      $contents = '';
    } else {
      curl_close($ch);
    }

    $output= json_decode($geocode);  

    return $output->results[0]->geometry->location;  
}

我的网站主机不允许fopen,切换到curl()。