Google Maps标记未显示javascript

德罗贡

我正在使用自定义地图上的标记进行挣扎。地图以正确的位置为中心,但标记未显示在该位置。有人已经解决了相同的问题吗?非常感谢您的帮助!

  function initialize() {
                var map_canvas = document.getElementById('map_canvas');
                var map_options = {
                    scrollwheel: false,
                    zoom: 16,
                    center: new google.maps.LatLng(51.840164,4.33244),
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: false,
                    mapTypeControlOptions: {
                        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                        position: google.maps.ControlPosition.TOP
                    },
                    panControl: false,
                    panControlOptions: {
                        position: google.maps.ControlPosition.TOP_RIGHT
                    },
                    zoomControl: true,
                    zoomControlOptions: {
                        style: google.maps.ZoomControlStyle.LARGE,
                        position: google.maps.ControlPosition.LEFT_CENTER
                    },
                    scaleControl: false,
                    scaleControlOptions: {
                        position: google.maps.ControlPosition.TOP_LEFT
                    },
                    streetViewControl: false,
                    streetViewControlOptions: {
                        position: google.maps.ControlPosition.LEFT_CENTER
                    }
                  }

                var map = new google.maps.Map(map_canvas, map_options)
              }

        function addMarker(feature) {
                  var marker = new google.maps.Marker({
                    position: feature.position,
                    icon: 'locationpointer.png',

                    map: map_canvas
                  });
                }

                var features = [
                  {
                    position: new google.maps.LatLng(51.840164,4.33244),
                  }
                ];

                for (var i = 0, feature; feature = features[i]; i++) {
                  addMarker(feature);
                }




        google.maps.event.addDomListener(window, 'load', initialize);
地理编码

您需要在initialize函数中调用addMarker函数,以便在初始化地图而不是之前添加标记您有2个用于初始化标记的map属性的选项:1.使其成为全局属性;或2.将其传递给addMarker函数(如下所示)。还要注意,map_canvas是HTML DOM元素,而不是google.maps.Map,该变量的名称是“ map”。

function initialize() {
  var map_canvas = document.getElementById('map_canvas');
  var map_options = {
                scrollwheel: false,
                zoom: 16,
                center: new google.maps.LatLng(51.840164,4.33244),
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP
                },
                panControl: false,
                panControlOptions: {
                    position: google.maps.ControlPosition.TOP_RIGHT
                },
                zoomControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE,
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                scaleControl: false,
                scaleControlOptions: {
                    position: google.maps.ControlPosition.TOP_LEFT
                },
                streetViewControl: false,
                streetViewControlOptions: {
                    position: google.maps.ControlPosition.LEFT_CENTER
                }
              }

  var map = new google.maps.Map(map_canvas, map_options)

  var features = [
      {
        position: new google.maps.LatLng(51.840164,4.33244),
      }
  ];

  for (var i = 0, feature; feature = features[i]; i++) {
    addMarker(feature, map);
  }
}

function addMarker(feature, map) {
   var marker = new google.maps.Marker({
                position: feature.position,
                icon: 'locationpointer.png',
                map: map
   });
}

google.maps.event.addDomListener(window, 'load', initialize);

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章