var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(25.825421, 0.898438),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListeneer(map, 'click', function(x) {
ylat.value = x.latLng.lat();
ylong.value = x.latLng.lng();
});
}
var ylat = document.getElementById('lat');
var ylong = document.getElementById('loong');
function addMarker() {
var lat = ylat.value;
var loong = ylong.value;
if(!lat || !loong) return;
var coordinate = new google.maps.LatLng(lat, loong);
var marker = new google.maps.Marker({
map: map,
position: coordinate
});
}
google.maps.event.addDomListener(document.getElementById('search'), 'click', addMarker);
google.maps.event.addDomListener(window, 'load', initialize);
当我在Firefox上运行时,Firebug告诉我" TypeError:a为null"我已经在各种网站和这里查看了这个。我对如何修复它没有丝毫的线索。请帮忙!
答案 0 :(得分:4)
这一行存在两个问题:
google.maps.event.addDomListener(document.getElementById('search'), 'click', addMarker);
addMarker
在全局范围内不可用(而不是在执行此行时)<head/>
内,<body/>
内的元素尚未可用,document.getElementById('search')
将是null
。将行移至初始化的末尾应解决这两个问题。
此外,还有一个错字:
google.maps.event.addListeneer(map, 'click', function(x) {
//__________________________^