Maps Google API傳回錯誤的座標
P粉773659687
P粉773659687 2023-07-26 15:39:29
0
1
460
<p>我使用這個PHP函數來取得座標,但它們總是錯誤的。有時候接近,有時候離正確的位置很遠。 </p> <pre class="brush:php;toolbar:false;">function getCoordinates($address, $city, $postalCode, $region, $province) { $url = 'https://maps.googleapis.com/maps/api/geocode/json?'; $params = array( 'address' => urlencode($address), 'components' => urlencode("locality:$city|administrative_area:$province"), 'key' => 'my_key' ); $url .= http_build_query($params); $response = file_get_contents($url); $data = json_decode($response, true); if ($data['status'] === 'OK') { $latitude = $data['results'][0]['geometry']['location']['lat']; $longitude = $data['results'][0]['geometry']['location']['lng']; return array('latitude' => $latitude, 'longitude' => $longitude); } else { return false; } }</pre> <p>我嘗試以多種方式使用參數,但始終得到相同的結果:錯誤的座標。 </p><p>測試參數如下:</p><p><br /></p> <pre class="brush:php;toolbar:false;">$address = 'VIA DUCHESSA JOLANDA' $city = 'MONCRIVELLO' $province = 'VC' $postalcode = '13040'</pre> <p>結果是緯度:45.0742756,經度:7.6613655</p><p>而正確的應該是:緯度:45.3307055,經度:7.9960788</p<< /strong></p>
P粉773659687
P粉773659687

全部回覆(1)
P粉386318086

很抱歉,使用這個新版本後,座標看起來是正確的。

function getCoordinates1($address, $city, $postalCode, $region, $province)
{
    $url = 'https://maps.googleapis.com/maps/api/geocode/json?';
    $addressString = "$address, $postalCode, $city, $province, $region";
    $params = array(
        'address' => urlencode($addressString),
        'key' => 'my-code'
    );
    $url .= http_build_query($params);
    $response = file_get_contents($url);
    $data = json_decode($response, true);
    if ($data['status'] === 'OK') {
        $latitude = $data['results'][0]['geometry']['location']['lat'];
        $longitude = $data['results'][0]['geometry']['location']['lng'];
        return array('latitude' => $latitude, 'longitude' => $longitude);
    } else {
        return false;
    }
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板