Google地图标记未显示javascript

时间:2013-09-25 15:20:55

标签: javascript google-maps google-maps-api-3 maps google-maps-markers

我正在努力使用自定义地图上的标记。地图位于正确的位置,但标记不会显示在该位置。有人有同样的问题已经解决了吗? 非常感谢你的帮助!

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

2 个答案:

答案 0 :(得分:0)

您缺少将标记添加到地图中。 使用marker.setMap(map)或在尝试时通过标记属性传递它。 但是在你的代码中map和map_canvas变量不在全局范围内,这意味着你的addMarker函数无法添加它并映射:map_canvas为null

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

您必须稍微更改代码才能将地图传递到addMarker函数。

答案 1 :(得分:0)

您需要在初始化函数中调用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);