最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Google Maps API Javascript; TypeError a is null - Stack Overflow

programmeradmin6浏览0评论
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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

There are 2 issues with this line:

google.maps.event.addDomListener(document.getElementById('search'), 'click', addMarker);
  1. the function addMarker is not available in global scope(and not at the time when this line will be executed).
  2. (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 be null.

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) {
//__________________________^
发布评论

评论列表(0)

  1. 暂无评论