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);
When I run this on Firefox, Firebug tells me "TypeError: a is null" I have looked this up on various websites and on here as well. I don't have the slightest clue on how to fix it. Please help!
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);
When I run this on Firefox, Firebug tells me "TypeError: a is null" I have looked this up on various websites and on here as well. I don't have the slightest clue on how to fix it. Please help!
Share Improve this question asked Mar 11, 2014 at 3:21 user3223207user3223207 431 gold badge2 silver badges9 bronze badges1 Answer
Reset to default 4There are 2 issues with this line:
google.maps.event.addDomListener(document.getElementById('search'), 'click', addMarker);
the function.addMarker
is not available in global scope(and not at the time when this line will be executed)- (I guess that's the problem here) assuming the script is placed inside the
<head/>
, the elements inside the<body/>
are not available yet,document.getElementById('search')
will benull
.
Moving the line to the end of initialize should fix both problems.
Additionally, there is a typo:
google.maps.event.addListeneer(map, 'click', function(x) {
//__________________________^