gpt4 book ai didi

javascript - 谷歌地图类型错误: a is null

转载 作者:行者123 更新时间:2023-11-28 07:31:01 25 4
gpt4 key购买 nike

我必须在我的网络应用程序中使用谷歌地图。问题是我收到一条错误,提示 TypeError: a is null。这是我在窗口加载时调用谷歌地图的方式:

var lat;
var long;
var map;

function geoloc() {//on window load get the user latitude and longitue and pass the values to "successPosition" function if user allow the geolocation other wise call "showError" function to gave static latitude and longitude.


if (navigator.geolocation) {
var optn = {
enableHighAccuracy: true,
timeout: 0,
maximumAge: 0
};

navigator.geolocation.getCurrentPosition(successPosition, showError, optn);

} else {
alert('Geolocation is not supported in your browser');
}
}

function successPosition(position) { //get the latitude and longitude by geolocation function


lat = position.coords.latitude;
long = position.coords.longitude;

initialize(lat, long);//calling the initialize function
}

function initialize(lat, long) { //initialize the map

var myLatlng = new google.maps.LatLng(lat, long);

var mapOptions = {//maps options the zoom level and map center
zoom: 4,
center: myLatlng

}

map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);


var marker = new google.maps.Marker({//create the marker on the map
position: myLatlng,
map: map,
title: 'Hello World!'
});
}


function showError() {//pass the values to "initialize" function if the user dosn't allow the geolocation

initialize(31.5497, 74.3436);
}
google.maps.event.addDomListener(window, 'load', geoloc);//on window load calls the geoloc function to get the user latitude and longitud

当我在 Firefox 上运行这个程序时,Firebug 告诉我“TypeError: a is null”。我不知道如何修复它。请帮忙!

最佳答案

我尝试了你的代码,它确实创建了一个谷歌地图。我想知道您在运行脚本之前是否正确包含了 Maps API JavaScript。

<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=ENTER-YOUR-UNIQUE-KEY-HERE">
</script>

也许该网站的其他部分存在干扰的错误?

关于javascript - 谷歌地图类型错误: a is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29125857/

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