Javascript 如何修复错误:请求被拒绝未找到映射。使用php在google地图中查找地址时

Javascript 如何修复错误:请求被拒绝未找到映射。使用php在google地图中查找地址时,javascript,php,google-maps,maps,geocoding,Javascript,Php,Google Maps,Maps,Geocoding,这是我的代码 <!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 D

这是我的代码

<!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>

使用PHP实时演示Google地图地理编码示例
正在加载地图。。。
地图显示了大概的位置。
函数init_map(){
变量myOptions={
缩放:14,
中心:新google.maps.LatLng(,),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“gmap_画布”),myOptions);
marker=新的google.maps.marker({
地图:地图,
位置:新的google.maps.LatLng(,)
});
infowindow=新建google.maps.infowindow({
内容:“”
});
google.maps.event.addListener(标记“单击”,函数(){
信息窗口。打开(地图、标记);
});
信息窗口。打开(地图、标记);
}
google.maps.event.addDomListener(窗口'load',初始化映射);

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

“请求被拒绝”表示您的请求通常被拒绝 因为缺少传感器参数

传感器(必需)-指示是否存在地理编码请求 来自带有位置传感器的设备。此值必须为 真假


将此添加到请求中:
&sensor=true

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

我得到:

{ “错误消息”:“此API项目无权使用此API。请确保此API在Google开发者控制台中激活:”, “结果”:[], “状态”:“请求被拒绝” }


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

它在哪里?在variabel url中@Leo R.传感器参数为,请为您的答案中引用的文本提供参考。您从何处获得答案中引用的文本?请提供官方文档的链接,说明(据我所知,这是不正确的)。@geocodezip这是我从中获得代码的地方。这个答案不再正确。请看:我已经修复了api,但在我搜索之后,它仍然显示相同的错误。您修复api是什么意思?“相同的错误”是指我回答中的错误信息还是来自代码的“找不到地图”?您在请求中使用的地址是什么?
https://maps.googleapis.com/maps/api/geocode/json?address=Nebraska&key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback