Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我有一个脚本,用于向交互式谷歌地图添加多个标记。我希望所有的标记都包含不同的信息窗口_Javascript_Google Maps_Google Maps Markers_Infowindow - Fatal编程技术网

Javascript 我有一个脚本,用于向交互式谷歌地图添加多个标记。我希望所有的标记都包含不同的信息窗口

Javascript 我有一个脚本,用于向交互式谷歌地图添加多个标记。我希望所有的标记都包含不同的信息窗口,javascript,google-maps,google-maps-markers,infowindow,Javascript,Google Maps,Google Maps Markers,Infowindow,我已经创建了下面的代码。这段代码生成一个带有不同标记的谷歌地图。我似乎没有正确地理解代码,因此每个标记都会在鼠标单击时显示不同的信息窗口。是否有某种方法可以在for循环中添加代码,以便使用不同内容的不同信息窗口构建所有标记 <body onload="initialize()"> <div id="map_canvas" style="width: 800px; height: 500px;"></div> <script type="tex

我已经创建了下面的代码。这段代码生成一个带有不同标记的谷歌地图。我似乎没有正确地理解代码,因此每个标记都会在鼠标单击时显示不同的信息窗口。是否有某种方法可以在for循环中添加代码,以便使用不同内容的不同信息窗口构建所有标记

<body onload="initialize()">

  <div id="map_canvas" style="width: 800px; height: 500px;"></div>

  <script type="text/javascript">
    function initialize() {
      var myOptions = {
        zoom: 2,
        center: new google.maps.LatLng(23.241346, 18.281250),
      mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"),
                                    myOptions);

      setMarkers(map, locations);
    }

     var locations = [
       ['AU', -37.850565, 144.980289 , 4],
       ['AS', 48.1670845, 16.3465254, 5],
       ['BE', 50.8826906, 4.4570261, 3],
       ['BR', -23.5004937, -46.8482093, 2],
       ['CZ', 50.0878114, 14.4204598, 1],
       ['DM', 55.6710507, 12.4401635, 6],
       ['FI', 60.2101064, 24.8251788, 7],
       ['FR', 48.8744779, 2.1668675, 8],
       ['GE', 51.19423, 6.70568, 9],
       ['GR', 38.0433281, 23.797971, 10]
     ];

     function setMarkers(map, locations) {
       var image = new google.maps.MarkerImage('punaise.png',
         new google.maps.Size(30, 30),
         new google.maps.Point(0,0),
         new google.maps.Point(0, 32));
       var shadow = new google.maps.MarkerImage('schaduw.png',
         new google.maps.Size(30, 30),
         new google.maps.Point(0,0),
         new google.maps.Point(0, 32));

       var shape = {
         coord: [1, 1, 1, 20, 18, 20, 18 , 1],
         type: 'poly'
       };

       for (var i = 0; i < locations.length; i++) {
         var entity = locations[i];
         var myLatLng = new google.maps.LatLng(entity[1], entity[2]);
         var marker = new google.maps.Marker({
           position: myLatLng,
           map: map,
           shadow: shadow,
           icon: image,
           shape: shape,
           title: entity[0],
           zIndex: entity[3],
         });
       }
    }
  </script>

函数初始化(){
变量myOptions={
缩放:2,
中心:新google.maps.LatLng(23.24134618.281250),
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(document.getElementById(“map_canvas”),
肌肽);
设置标记(地图、位置);
}
变量位置=[
[AU',-37.850565144.980289,4],
[AS',48.1670845,16.3465254,5],
[BE',50.8826906,4.4570261,3],
[BR',-23.5004937,-46.8482093,2],
[CZ',50.0878114,14.4204598,1],
[DM',55.6710507,12.4401635,6],
[FI',60.2101064,24.8251788,7],
[FR',48.8744779,2.1668675,8],
[GE',51.19423,6.70568,9],
[GR',38.0433281,23.797971,10]
];
功能设置标记(地图、位置){
var image=new google.maps.MarkerImage('punaise.png',
新google.maps.Size(30,30),
新google.maps.Point(0,0),
新的google.maps.Point(0,32));
var shadow=new google.maps.MarkerImage('schaduw.png',
新google.maps.Size(30,30),
新google.maps.Point(0,0),
新的google.maps.Point(0,32));
变量形状={
坐标:[1,1,1,20,18,20,18,1],
类型:“poly”
};
对于(变量i=0;i
这是相对直截了当的;在for循环中,您希望初始化info窗口,类似于:

for (var i = 0; i < locations.length; i++) {
    var entity = locations[i];
    var myLatLng = new google.maps.LatLng(entity[1], entity[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        title: entity[0],
        zIndex: entity[3],
    });
    infoWindow = new google.maps.InfoWindow({
        content: document.getElementById("overlayContent" + [i]).innerHTML
    });
}

currentInfoWindow
检查确保一次只打开一个覆盖

谢谢!在您的示例中,是否可以向每个窗口添加不同的信息?当然,只需在每个div中设置不同的内容:)单击标记,不会生成窗口。这些div在谷歌地图下都显示得很好。没有运行错误。。给你的建议试试看(我想这将是最好的,以确定什么是错误的)建议是工作代码从一个活的项目!您确定控制台中没有错误吗?你用的是什么浏览器?我用的是mozilla firefox。也许您提供的工作代码没有与我的代码进行应有的交互?奇怪的你有没有试过把你的代码添加到我的代码中,然后把整个代码放到浏览器中?
google.maps.event.addListener(marker, "click", function () {

    if (currentInfoWindow) {
        currentInfoWindow.close();
    }
    infoWindow.open(gmap, marker);
    currentInfoWindow = infoWindow;
});