【问题标题】:multiple custom markers (icons) with different infowindows具有不同信息窗口的多个自定义标记(图标)
【发布时间】:2017-04-17 16:20:10
【问题描述】:

我正在创建一个地图,我将在其中添加多个自定义标记,并且我希望每个标记都有不同的信息窗口。 问题是每个标记都有一个不同的图标(您在图像中看到的那些编号的点),这是一个在 GoogleMaps API 代码示例中没有解释的示例。我的意思是,他们向您解释了如何创建 infowindows,但仅在您使用变量 marker 而不是变量 icons 的情况下。因此,我不知道应该在哪里添加信息窗口的代码。该网站如下所示:

website screenshot

 <script>
  var map;
  function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
      zoom: 13,
      backgroundColor: '#000000',
      center: new google.maps.LatLng(41.404998, 2.210517),
      mapTypeId: 'roadmap',
      streetViewControl: false,

    });

    var iconBase = 'images/numbers/';
    var icons = {
      001: {
        icon: iconBase + '01.png'
      },
      002: {
        icon: iconBase + '02.png'
      }
    };

    function addMarker(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,
        map: map
      });
    }

    var features = [
      {
        position: new google.maps.LatLng(41.404998, 2.210517),
        type: 001
        //Barcelona 1
      }, {
        position: new google.maps.LatLng(41.404371, 2.179131),
        type: 002
        //Barcelona 2
      }
    ];

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

</script>

【问题讨论】:

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


    【解决方案1】:

    如果我可以回答 Josep 的最后一个问题(我是 Stack Overflow 的新手,请原谅我的不准确之处),我更新了小提琴 http://jsfiddle.net/t7trcpv2/1/,在“功能”对象中添加了一个“标题”属性,您可以在其中输入您的自己的数据

    var map;
    var infowindow;
    
    function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        backgroundColor: '#000000',
        center: new google.maps.LatLng(41.404998, 2.210517),
        mapTypeId: 'roadmap',
        streetViewControl: false,
      });
      infowindow = new google.maps.InfoWindow();
      var iconBase = 'images/numbers/';
      var icons = {
        001: {
          icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
        },
        002: {
          icon: "http://maps.google.com/mapfiles/ms/micons/green.png"
        }
      };
    
      function addMarker(feature) {
        var marker = new google.maps.Marker({
          position: feature.position,
          icon: icons[feature.type].icon,
          map: map,
    
        });
        // must be a string (or a DOM node).
        var content = "" + feature.title
        google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
          return function(evt) {
            infowindow.setContent(content);
            infowindow.open(map, marker);
          };
        })(marker, content, infowindow));
    
      }
    
      var features = [{
        position: new google.maps.LatLng(41.404998, 2.210517),
        type: 001,
        title:'title1'
          //Barcelona 1
      }, {
        position: new google.maps.LatLng(41.404371, 2.179131),
        type: 002,
        title: 'title2'
          //Barcelona 2
      }];
    
      for (var i = 0, feature; feature = features[i]; i++) {
        addMarker(feature);
      };
    }
    google.maps.event.addDomListener(window, "load", initialize);
    

    【讨论】:

    • 您的建议是正确的,但您应该添加适当的示例代码(不一定是完全原创的).. 链接答案可能在示例代码保留时失效
    【解决方案2】:

    尝试以这种方式使用闭包

      var features = [
        {
          position: new google.maps.LatLng(41.404998, 2.210517),
          type: 001
          //Barcelona 1
        }, {
          position: new google.maps.LatLng(41.404371, 2.179131),
          type: 002
          //Barcelona 2
        }
      ];
    
    
    
    
       function addMarker(feature) {
          var marker = new google.maps.Marker({
            position: feature.position,
            icon: icons[feature.type].icon,
            map: map
          });
    
        var infowindow = new google.maps.InfoWindow()
    
        var content  = "" + feature.type
    
        google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
          return function() {
            infowindow.setContent(content);
            infowindow.open(map,marker);
          };
        })(marker,content,infowindow)); 
    
      }
    

    您可以通过这种方式更改功能

    var features = [
      {
        position: new google.maps.LatLng(41.404998, 2.210517),
        type: 001,
        message:  'my firts message in info window'
        //Barcelona 1
      }, {
        position: new google.maps.LatLng(41.404371, 2.179131),
        type:  002,
        message:  'my second message in info window'
        //Barcelona 2
      }
    ];
    

    或者你可以添加一个新的属性

    你应该使用

     function addMarker(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,
        map: map
      });
    
    var infowindow = new google.maps.InfoWindow()
    
    var content  = "" + feature.message
    
    google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
      return function() {
        infowindow.setContent(content);
        infowindow.open(map,marker);
      };
    })(marker,content,infowindow)); 
    

    【讨论】:

    • content 必须是字符串(或 DOM 节点)(数字会导致信息窗口空白和错误消息 InvalidValueError: setContent: not a string;)。 working fiddle
    • @geocodezip .正确(像往常一样)谢谢..答案更新了..我没有注意到它只是一个数字
    • @geocodezip 非常感谢!现在 infowindows 确实出现了。但它们显示数字:图标 001 为“1”,图标 002 为“2”。我希望它们显示不同的信息,但不知道我必须在哪里放置内容字符串(我想我必须这样做..?)
    • @JosepFonti 。如果您查看答案中的示例,您会发现在 var 内容中为 infowindow 分配的信息来自 feature.type .. 因此,如果您需要其他信息,请在数组中更改此值或在 fetaure 中添加和管理新属性跨度>
    • @JosepFonti 这是因为 infowindow 需要文本,但 feature.type 会自动转换为数字以避免这种情况 sintax ""+ feature.type 强制 javascript 将结果转换为字符串,因此 inforwindow 接受内容...希望很明确..
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2021-12-15
    • 2018-01-19
    • 2013-04-17
    • 2020-04-12
    • 2013-08-19
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多