簡體   English   中英

從服務器端到客戶端的Google Geocoding API問題

[英]Google Geocoding API, moving from server-side to client-side, issues

我有一個正在運行的商店定位器腳本。 用戶輸入其地址,然后對其地址進行地理編碼,然后將經緯度傳遞給MySQL查詢,該查詢選擇地址x英里內的位置。 今天,盡管我遇到了以下地理編碼服務器端請求的第一個問題:

$apiURL = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address,$city,$state,$zipcode&region=$country&key=$key");
$output= json_decode($apiURL);

$lat_lon->lat = $output->results[0]->geometry->location->lat;
$lat_lon->lon = $output->results[0]->geometry->location->lng;

問題是我超過了2500個請求限制。 因此,我閱讀了有關客戶端請求的文章,以此作為克服此問題的一種方法。 我還閱讀了有關緩存結果的信息,但老實說,我相信我每天會收到2500多個不同的地址請求。 所以,我想和客戶端一起去。

幾個小時后,我完全不熟悉Google Maps API。 我想出了以下javascript:

function GetLocation() {
    var geocoder = new google.maps.Geocoder();
   geocoder.geocode( { 'address': <? echo $_POST['zipcode'] ?>}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        lat = results[0].geometry.location.lat();
        lng = results[0].geometry.location.lng();
    } 
});
};

我的第一個問題是,我是否正確編寫了此代碼,以直接替換服務器端代碼,即,lat和lng從服務器端請求中返回它們的等效項($ lat_lon-> lat和$ lat_lon-> lng) ?

我的第二個問題是,究竟如何將lat和lng變量傳遞給MySQL查詢,該查詢位於另一個文件中,並用PHP編碼? 使用服務器端編碼,地理編碼功能與數據庫查詢位於同一php文件中,因此引用$ lat_lon->很簡單。 但是,我之前從未做過此類編碼,而我拖延數小時的網絡工作卻無濟於事。 我被打敗了

任何人都可以闡明一下嗎?

我不知道是否有什么區別,但查詢字符串是:

$query = "SELECT *,(((acos(sin(($lat*$pi/180)) * sin((lat*$pi/180)) + cos(($lat*$pi/180)) *  cos((lat*$pi/180)) * cos((($lon - lon)*$pi/180))))*180/$pi)*60*1.423) as distance FROM locations HAVING distance <= $radius AND $category_search AND (expiration_date >= NOW() || expiration_date = '0000-00-00 00:00:00') ORDER BY distance ASC $results_limit";

謝謝。

好的,所以我得到了一個地圖編碼器,可以幫我解決這個問題,因為這已經超出了我的聯盟范圍,看到他所做的一切,我很高興自己沒有自己做。

在帶有搜索表單的頁面上,我具有自動完成功能,他將其更改為:

function fillInAddress() {
        var place = autocomplete.getPlace();

// HE ADDED THE NEXT 5 LINES TO THE FUNCTION
        var lat = place.geometry.location.lat();
        var lng = place.geometry.location.lng();
        $(".storelocator_body form").find("input.geometry").remove();
        $(".storelocator_body form").append("<input type='hidden' class='geomtery' name='lat' value='" + lat + "'>");
        $(".storelocator_body form").append("<input type='hidden' class='geomtery' name='lng' value='" + lng + "'>");

        for (var component in componentForm) {
            document.getElementById(component).value = '';
            document.getElementById(component).disabled = false;
        }
        if (place.address_components != undefined && place.address_components != null) {
            for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                document.getElementById(addressType).value = val;
            }
        }
        }
    }

然后在php文件中,我擁有原始的服務器端地址解析,他將其更改為:

if (isset($_POST['lat']) && isset($_POST['lng'])) {
      $lat_lon->lat = $_POST['lat'];
      $lat_lon->lon = $_POST['lng'];
  } else {

//THESE NEXT 4 LINES ARE THE ORIGINAL CODE, WHICH HE SUBSEQUENTLY ADDED TO
      $apiURL = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address,$city,$state,$zipcode&region=$country&key=$key");
      $output= json_decode($apiURL);
      $lat_lon->lat = $output->results[0]->geometry->location->lat;
      $lat_lon->lon = $output->results[0]->geometry->location->lng;
  }

就那么簡單。 現在工作。 希望這可以幫助任何人像我一樣尋找可能的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM