Google Maps api标记未定义错误

een

我需要在地图上放置多个标记,然后才能点击它们。然后,应该在我的网站内启动某个JS,为此,我正在使用以下代码:

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);

但这给了我Uncaught ReferenceError: marker is not defined错误。我究竟做错了什么?

蒂姆·比格莱森

您已var marker在函数内部定义了getAddress,因此该变量仅在此范围内可用。getAddress像这样将变量定义移出

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");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps API 3-类型错误:a未定义

来自分类Dev

Google Maps API返回“未定义”

来自分类Dev

Google Maps Api v3标记。Google未定义

来自分类Dev

Google Maps API 3中未定义标记的javascript数组

来自分类Dev

Google Maps API v3 Heatmaps错误:“无法读取未定义的属性'HeatmapLayer'”

来自分类Dev

Google Maps JS API(v3)InfoWindow脚本错误-JSON未定义

来自分类Dev

Google Maps api V3,添加ajax后,这在getCenter()上是未定义的错误

来自分类Dev

Google Maps集成中的“未定义rd”错误

来自分类Dev

Google Maps集成中的“未定义rd”错误

来自分类Dev

Google Maps API gm_bindings_未定义

来自分类Dev

Google Maps API gm_bindings_未定义

来自分类Dev

google.maps.places未定义

来自分类Dev

Google Maps Geocoder未定义

来自分类Dev

未定义Google Maps功能

来自分类Dev

Google Maps Geocoder未定义

来自分类Dev

为什么Google Maps API中未定义我的Maps对象

来自分类Dev

很难让Google Maps在Ionic中显示-“未定义Google”

来自分类Dev

Google Maps API JS V3:infowindow.getPosition()==未定义?

来自分类Dev

Google Maps API(使用gmapsjs)时,setCenter抛出“未定义不是函数”吗?

来自分类Dev

为什么在使用 Google Maps API 时全局设置的 JS 变量显示为未定义?

来自分类Dev

Google Maps API标记刷新

来自分类Dev

群组标记-Google Maps API

来自分类Dev

ReferenceError:使用angular-google-maps时未定义_

来自分类Dev

带有Dart的Google Maps:保留功能未定义

来自分类Dev

Google Maps-从未定义的函数返回getPosition

来自分类Dev

Google Maps刷新标记

来自分类Dev

Google Maps标记动作

来自分类Dev

筛选标记Google Maps

来自分类Dev

错误:Google Maps API错误

Related 相关文章

热门标签

归档