我必须使用谷歌地图作为我的网络应用程序。问题是我得到一个错误,说明TypeError:a为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为null"。我对如何修复它没有丝毫的线索。请帮忙!
答案 0 :(得分:0)
我尝试了您的代码并确实创建了Google地图。我想知道你是否在运行脚本之前正确地包含了Maps API JavaScript。
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=ENTER-YOUR-UNIQUE-KEY-HERE">
</script>
也许该网站的另一部分存在干扰错误?