【问题标题】:how to fixed ERROR: REQUEST_DENIEDNo map found. when finding address in google map using php如何修复错误:REQUEST_DENIED 未找到地图。使用 php 在谷歌地图中查找地址时
【发布时间】:2018-09-21 09:20:10
【问题描述】:

这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Live Demo of Google Maps Geocoding Example with PHP</title>
</head>
<?php
if($_POST){

// get latitude, longitude and formatted address
$data_arr = geocode($_POST['address']);

// if able to geocode the address
if($data_arr){

    $latitude = $data_arr[0];
    $longitude = $data_arr[1];
    $formatted_address = $data_arr[2];

    ?>

    <!-- google map will be shown here -->
    <div id="gmap_canvas">Loading map...</div>
    <div id='map-label'>Map shows approximate location.</div>

    <!-- JavaScript to show google map -->
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback"></script>
    <script type="text/javascript">
        function init_map() {
            var myOptions = {
                zoom: 14,
                center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
            marker = new google.maps.Marker({
                map: map,
                position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
            });
            infowindow = new google.maps.InfoWindow({
                content: "<?php echo $formatted_address; ?>"
            });
            google.maps.event.addListener(marker, "click", function () {
                infowindow.open(map, marker);
            });
            infowindow.open(map, marker);
        }
        google.maps.event.addDomListener(window, 'load', init_map);
    </script>

    <?php

    // if unable to geocode the address
}else{
    echo "No map found.";
}
}
?>
<body>
<form action="" method="post">
    <input type='text' name='address' placeholder='Enter any address here' />
    <input type='submit' value='Geocode!' />
</form>
<?php
// function to geocode address, it will return false if unable to geocode address
function geocode($address){

// url encode the address
$address = urlencode($address);

// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback";

// get the json response
$resp_json = file_get_contents($url);

// decode the json
$resp = json_decode($resp_json, true);

// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){

    // get the important data
    $lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
    $longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
    $formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";

    // verify if data is complete
    if($lati && $longi && $formatted_address){

        // put the data in the array
        $data_arr = array();

        array_push(
            $data_arr,
            $lati,
            $longi,
            $formatted_address
        );

        return $data_arr;

    }else{
        return false;
    }

}

else{
    echo "<strong>ERROR: {$resp['status']}</strong>";
    return false;
}
}
?>
</body>
</html>

我从这个网站得到这个代码https://www.codeofaninja.com/2014/06/google-maps-geocoding-example-php.html 这是我的网站,显示此代码http://econmarket.000webhostapp.com/tracking.php 我知道如何解决它,但这就是我所得到的,每次我在文本框中输入地址时,它总是显示错误 idk 为什么。

【问题讨论】:

    标签: javascript php google-maps maps geocoding


    【解决方案1】:

    您是否在请求中指定了传感器参数?

    “REQUEST_DENIED”表示您的请求被拒绝,一般 因为缺少传感器参数。

    sensor (required) — 指示地理编码请求是否 来自带有位置传感器的设备。该值必须是 真假

    在请求中添加这个:&amp;sensor=true

    【讨论】:

    【解决方案2】:

    如果我在浏览器中输入您的请求(使用任意地址):

    https://maps.googleapis.com/maps/api/geocode/json?address=Nebraska&key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback
    

    我明白了:

    { "error_message" : "此 API 项目无权使用此 API。请确保在 Google Developers Console 中激活此 API:https://console.developers.google.com/apis/api/geocoding_backend?project=_", “结果” : [], “状态”:“REQUEST_DENIED” }

    为该密钥授权 Google 地图地理编码器(或者更好的是,禁用或删除该密钥,因为您公开披露了该密钥,并创建一个仅限于您的站点且已获得地理编码 API 授权的新密钥)

    【讨论】:

    • 我已经修复了我的 api,但是在我搜索之后它仍然显示相同的错误
    • 你修复了你的api是什么意思? “相同的错误”是指我的答案中的错误消息还是“未找到地图”。来自你的代码?您在请求中使用的地址是什么?
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多