0

我正在与自定义地图上的标记作斗争。地图以正确的位置为中心,但标记未显示在该位置。有人已经解决了同样的问题吗?非常感谢您的帮助!

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

2 回答 2

0

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

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

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

于 2013-09-25T15:23:33.807 回答
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);
于 2013-09-25T15:44:57.550 回答