Javascript 错误:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44878472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
提问by ravneet
function initAutocomplete() {
var lat=document.getElementById('lat').value;
var lng=document.getElementById('lng').value;
console.log(lat);
console.log(lng);
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat:lat, lng:lng},
zoom: 13,
mapTypeId: 'roadmap'
});}
it gives me the following error :
它给了我以下错误:
error:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
错误:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
回答by Andreas
The .valueattribute of a HTMLInputElementreturns the value as a string.
a 的.value属性HTMLInputElement以字符串形式返回值。
You have to parse the content of latand lngwith parseFloat()before passing it to the maps API
在将其传递给地图 API 之前,您必须解析lat和lngwith的内容parseFloat()
function initAutocomplete() {
var lat = parseFloat(document.getElementById('lat').value);
var lng = parseFloat(document.getElementById('lng').value);
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: lat,
lng: lng
},
zoom: 13,
mapTypeId: 'roadmap'
});
}
回答by anasmorahhib
Just add "+" before your variables:
只需在变量前添加“+”:
function initAutocomplete() {
var lat=document.getElementById('lat').value;
var lng=document.getElementById('lng').value;
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: +lat, lng: +lng},
zoom: 13,
mapTypeId: 'roadmap'
});}

