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

var map;function initialize() {var mapOptions = {center: new google.maps.LatLng(25.825421, 0.898438),z

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) {
//__________________________^

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744408717a4572763.html

相关推荐

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

    var map;function initialize() {var mapOptions = {center: new google.maps.LatLng(25.825421, 0.898438),z

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信