gpt4 book ai didi

javascript - 使用 JavaScript 获取纬度/经度值时出现 MissingKeyMapError

转载 作者:行者123 更新时间:2023-12-02 14:27:13 25 4
gpt4 key购买 nike

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

错误:

Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error

这是我的代码:

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

我在控制台中收到这些错误。这里我在本地主机中运行代码。

最佳答案

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

谷歌搜索了一下我发现了这个here (及官方公告http://googlegeodevelopers.blogspot.com.au/2016/06/building-for-scale-updates-to-google.html)

If you are using the Google Maps API on localhost or your domain was not active prior to June 22nd, 2016, it will require a key going forward. To fix this problem, please see the Google Maps APIs documentation to get a key and add it to your application: https://developers.google.com/maps/documentation/javascript/get-api-key

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]);
}
});
}
};

function getLatLon() {
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 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" onclick="getLatLon();">Get LatLong</button>
<div>
<p>Latitude:
<input type="text" id="latitude" readonly />
</p>
<p>Longitude:
<input type="text" id="longitude" readonly />
</p>
</div>
</div>

关于javascript - 使用 JavaScript 获取纬度/经度值时出现 MissingKeyMapError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com