使用JavaScript获取纬度/经度值时获取MissingKeyMapError

使用JavaScript获取纬度/经度值时获取MissingKeyMapError,javascript,google-maps,Javascript,Google Maps,我在尝试使用JavaScript从地址获取纬度和经度值时遇到以下错误 错误: 谷歌地图API错误:MissingKeyMapError 我在下面解释我的代码 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <div> <h3> Enter an adress and press the button

我在尝试使用JavaScript从地址获取纬度和经度值时遇到以下错误

错误:

谷歌地图API错误:MissingKeyMapError

我在下面解释我的代码

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div>
     <h3> Enter an adress and press the button</h3>

    <input id="address" type="text" placeholder="Enter address here" />
    <button id="btn">Get LatLong</button>
    <div>
        <p>Latitude:
            <input type="text" id="latitude" readonly />
        </p>
        <p>Longitude:
            <input type="text" id="longitude" readonly />
        </p>
    </div>
</div>
<script>
function getLatitudeLongitude(address,callback) {
    // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
    address = address || 'Ferrol, Galicia, Spain';
    // Initialize the Geocoder
    geocoder = new google.maps.Geocoder();
    if (geocoder) {
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                callback(results[0]);
            }
        });
    }
}
document.getElementById('btn').click=function(){
    var address = document.getElementById('address').value;
    getLatitudeLongitude(address,function(result){
         console.log('result',result);
         document.getElementById('latitude').value = result.geometry.location.lat();
         document.getElementById('longitude').value = result.geometry.location.lng();
    });
}
</script>

输入地址并按下按钮
拉特朗
纬度:

经度:

函数getLatitudeLongitude(地址,回调){ //如果未提供地址,请使用默认值“Ferrol,Galicia,西班牙” 地址=地址| |‘西班牙加利西亚费罗尔’; //初始化地理编码器 geocoder=新的google.maps.geocoder(); if(地理编码器){ 地理编码({ “地址”:地址 },功能(结果、状态){ if(status==google.maps.GeocoderStatus.OK){ 回调(结果[0]); } }); } } document.getElementById('btn')。单击=函数(){ var address=document.getElementById('address')。值; GetLatitudeLoguide(地址、函数(结果){ console.log('result',result); document.getElementById('latitude')。value=result.geometry.location.lat(); document.getElementById('longitude')。value=result.geometry.location.lng(); }); }

我在控制台中得到这些错误。这里我在localhost中运行代码

您的代码在不在本地主机上运行时工作

在谷歌上搜索一下,我发现了这一点(以及官方公告)

如果您在localhost上使用Google Maps API,或者您的域是 在2016年6月22日之前不活跃,需要钥匙 向前地要解决此问题,请参阅谷歌地图API 获取密钥并将其添加到应用程序的文档:

函数getLatitudeLongitude(地址,回调){ //如果未提供地址,请使用默认值“Ferrol,Galicia,西班牙” 地址=地址| |‘西班牙加利西亚费罗尔’; //初始化地理编码器 geocoder=新的google.maps.geocoder(); if(地理编码器){ 地理编码({ “地址”:地址 },功能(结果、状态){ if(status==google.maps.GeocoderStatus.OK){ 回调(结果[0]); } }); } }; 函数getLatLon(){ var address=document.getElementById('address')。值; GetLatitudeLoguide(地址、函数(结果){ //console.log('result',result); document.getElementById('latitude')。value=result.geometry.location.lat(); document.getElementById('longitude')。value=result.geometry.location.lng(); }); }

输入地址并按下按钮
拉特朗
纬度:

经度:


您的代码在不在本地主机上运行时有效

在谷歌上搜索一下,我发现了这一点(以及官方公告)

如果您在localhost上使用Google Maps API,或者您的域是 在2016年6月22日之前不活跃,需要钥匙 向前地要解决此问题,请参阅谷歌地图API 获取密钥并将其添加到应用程序的文档:

函数getLatitudeLongitude(地址,回调){ //如果未提供地址,请使用默认值“Ferrol,Galicia,西班牙” 地址=地址| |‘西班牙加利西亚费罗尔’; //初始化地理编码器 geocoder=新的google.maps.geocoder(); if(地理编码器){ 地理编码({ “地址”:地址 },功能(结果、状态){ if(status==google.maps.GeocoderStatus.OK){ 回调(结果[0]); } }); } }; 函数getLatLon(){ var address=document.getElementById('address')。值; GetLatitudeLoguide(地址、函数(结果){ //console.log('result',result); document.getElementById('latitude')。value=result.geometry.location.lat(); document.getElementById('longitude')。value=result.geometry.location.lng(); }); }

输入地址并按下按钮
拉特朗
纬度:

经度:


@user2314737:当我使用三台以上的本地主机时,没有出现错误,但结果没有出现。@satya我也稍微更改了您的代码,因为“单击”事件没有触发。它在我的代码段@user2314737中起作用:当我使用rathre而不是本地主机时,错误不会出现,但结果不会出现。@satya我也稍微更改了您的代码,因为“单击”事件没有触发。它在我的代码片段中起作用