位置查找器在PHP与经度和经度与谷歌api

时间:2017-07-07 09:14:45

标签: php google-maps location

我正在尝试使用地理编码API从谷歌地图获取位置。但经过一些使用后,api无效。

这是我的代码,我不知道发生了什么,我已经改变了api_key并尝试过,也没有从api获得

    function addressPicker($latitude, $longitude){
        /*$latitude = '9.537086';
        $longitude = '76.886407';*/
        $geolocation = $latitude.','.$longitude;

        $API_KEY = 'AIzaSyCV1OstieLoQIssF0tBwB6jYYz_I7w1FRA';
        $request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false&key='.$API_KEY;

        $file_contents = file_get_contents($request);
        $json_decode = json_decode($file_contents);
        $place_name = '';
        if(isset($json_decode->results[0])) {
            $response = array();
            $j = 0;
            foreach($json_decode->results[0]->address_components as $addressComponet) {
                $response2[] = $addressComponet->long_name;

                switch ($json_decode->results[0]->address_components[$j]->types[0]) {
                    case 'street_number':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'route':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'political':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'locality':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                }
                $j++;
            }
            return $place_name;
        }
    }

2 个答案:

答案 0 :(得分:1)

@Midhun请使用此代码。 我只是检查你的代码它的退货错误:

LIKE [error_message] =>对此API的请求必须通过SSL。使用“https://”而不是“http://”加载API。

 /*$latitude = '9.537086';
    $longitude = '76.886407';*/
    $geolocation = $latitude.','.$longitude;

    $API_KEY = 'AIzaSyCV1OstieLoQIssF0tBwB6jYYz_I7w1FRA';
    $request = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false&key='.$API_KEY;

    $file_contents = file_get_contents($request);
    $json_decode = json_decode($file_contents);
    $place_name = '';
    if(isset($json_decode->results[0])) {
        $response = array();
        $j = 0;
        foreach($json_decode->results[0]->address_components as $addressComponet) {
            $response2[] = $addressComponet->long_name;

            switch ($json_decode->results[0]->address_components[$j]->types[0]) {
                case 'street_number':
                    $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                    break;
                case 'route':
                    $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                    break;
                case 'political':
                    $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                    break;
                case 'locality':
                    $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                    break;
            }
            $j++;
        }
        return $place_name;
    }
}

所以我只需在https://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false&key='.$API_KEY;

中设置 https 即可

答案 1 :(得分:0)

确实,您的Google网址不正确。这个URL似乎很好:

https://maps.googleapis.com/maps/api/geocode/json?latlng=9.537086,76.886407&key=AIzaSyCV1OstieLoQIssF0tBwB6jYYz_I7w1FRA

function addressPicker($latitude, $longitude){
        /*$latitude = '9.537086';
        $longitude = '76.886407';*/
        $geolocation = $latitude.','.$longitude;

        $API_KEY = 'AIzaSyCV1OstieLoQIssF0tBwB6jYYz_I7w1FRA';
        $request = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false&key='.$API_KEY;

        $file_contents = file_get_contents($request);
        $json_decode = json_decode($file_contents);
        $place_name = '';
        if(isset($json_decode->results[0])) {
            $response = array();
            $j = 0;
            foreach($json_decode->results[0]->address_components as $addressComponet) {
                $response2[] = $addressComponet->long_name;

                switch ($json_decode->results[0]->address_components[$j]->types[0]) {
                    case 'street_number':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'route':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'political':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                    case 'locality':
                        $place_name .= $json_decode->results[0]->address_components[$j]->long_name . ', ';
                        break;
                }
                $j++;
            }
            return $place_name;
        }
    }