坐标功能有时不起作用

时间:2018-05-03 12:36:18

标签: php function maps coordinates

我使用此功能获取任何地址的坐标:

function getCoordinates($address) {
    $address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
    $address = str_replace("-", "+", $address); // replace all the "-"  with "+" sign to match with google search pattern
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address";
    $response = file_get_contents($url);
    $json = json_decode($response,TRUE); //generate array object from the response from the web
    return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}

有时它会起作用,有时它只能得到","对于同一地址。

我需要使用其他功能吗?

1 个答案:

答案 0 :(得分:0)

试试这个

$ url =" http://maps.google.com/maps/api/geocode/json?address="。$ address;