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

javascript - Google maps api marker is not defined error - Stack Overflow

programmeradmin0浏览0评论

I need to place multiple markers on the map with the ability to click on them. That should then fire up a certain JS within my website and for that I'm using the following code:

function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapCanvas = document.getElementById('map_canvas');
        var mapOptions = {
          center: new google.maps.LatLng(64.113598, -21.8569031),
          zoom: 12,
          scrollwheel: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(mapCanvas, mapOptions)

        function getAddress (address) {
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
              });
            } else {
              return;
            }
          });
        }

        getAddress("some address here");

      }
      google.maps.event.addListener(marker, 'click', function() {
        map.setZoom(8);
      });
      google.maps.event.addDomListener(window, 'load', initialize);

But this gives me Uncaught ReferenceError: marker is not defined error. What am I doing wrong?

I need to place multiple markers on the map with the ability to click on them. That should then fire up a certain JS within my website and for that I'm using the following code:

function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapCanvas = document.getElementById('map_canvas');
        var mapOptions = {
          center: new google.maps.LatLng(64.113598, -21.8569031),
          zoom: 12,
          scrollwheel: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(mapCanvas, mapOptions)

        function getAddress (address) {
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
              });
            } else {
              return;
            }
          });
        }

        getAddress("some address here");

      }
      google.maps.event.addListener(marker, 'click', function() {
        map.setZoom(8);
      });
      google.maps.event.addDomListener(window, 'load', initialize);

But this gives me Uncaught ReferenceError: marker is not defined error. What am I doing wrong?

Share Improve this question asked Apr 14, 2015 at 5:41 XeenXeen 7,01317 gold badges69 silver badges113 bronze badges 2
  • 1 If status != google.maps.GeocoderStatus.OK then the marker would not be getting initialized. Could you please look into this? – Tim Biegeleisen Commented Apr 14, 2015 at 5:43
  • @TimBiegeleisen yes, but it is getting initialized, I do have a marker on my map – Xeen Commented Apr 14, 2015 at 5:44
Add a ment  | 

1 Answer 1

Reset to default 2

You have defined the var marker inside the function getAddress, and therefore this variable will only be available within this scope. Please move the variable definition outside of getAddress like this:

var marker;

function initialize() {
    // ...

    function getAddress (address) {
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                    map: map,
                   position: results[0].geometry.location
                });
            } else {
                return;
            }
        });
    }

    getAddress("some address here");
}
发布评论

评论列表(0)

  1. 暂无评论