php中带经纬度的位置查找器,使用google api

php中带经纬度的位置查找器,使用google api,php,google-maps,location,Php,Google Maps,Location,我正试图用地理编码api从谷歌地图上获取位置。但是使用了一段时间后,api就不起作用了 这是我的代码,我不知道发生了什么,我已经改变了api_键并尝试了,也没有从api中得到任何东西 function addressPicker($latitude, $longitude){ /*$latitude = '9.537086'; $longitude = '76.886407';*/ $geolocation = $latitude.','.

我正试图用地理编码api从谷歌地图上获取位置。但是使用了一段时间后,api就不起作用了

这是我的代码,我不知道发生了什么,我已经改变了api_键并尝试了,也没有从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;
        }
    }

事实上,你的谷歌网址是不正确的。这个URL看起来不错:


@Midhun请使用此代码。 我只是在您的代码中签入它的返回错误:

与[error_message]=>类似,对该API的请求必须通过SSL。加载带有“https://”而不是“http://”的API


所以我只是在
https://maps.googleapis.com/maps/api/geocode/json?latlng=“.$geolocation.”&sensor=false&key=”.$API_-key

你是否使用API获得了简单的映射?首先,我使用此API获得了结果,现在没有数据Roger@Midhun,可以告诉我你在文件内容中得到了什么吗?请在地址栏中检查你的URL,并检查API返回的内容。实际上,它使用http返回结果,但在一些回复后,它停止并需要作为https并放置API密钥,现在它工作正常了,罗杰@米顿快乐的Codding兄弟
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;
        }
    }
 /*$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;
    }
}