首頁  >  問答  >  主體

Maps Google API傳回錯誤的座標

<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粉773659687P粉773659687427 天前411

全部回覆(1)我來回復

  • P粉386318086

    P粉3863180862023-07-27 14:56:55

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

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

    回覆
    0
  • 取消回覆