谷歌地图API V3 - infowindows @ markers在2个地图上

时间:2015-06-02 10:46:26

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

我正在制作Google Map API V3代码,我是JS的初学者,我有点困惑。

以下是我要做的事情:我有一个包含Lat / Lng,地址,图片,说明等的数据库

在我的最后一页上,我有2张地图,一张用于法国,一张用于巴黎。 如果lat / lng在巴黎,则标记在巴黎'地图,如果它在巴黎之外,它会在法国地图上显示。

到目前为止,我已经完成了我想要的工作,但我仍然坚持使用infoWindow :( 我可以打开它们,但不能自动关闭它们...... 我只想在同一时间打开一个信息窗口。

这是我的代码(只是JS部分):

(" mapidf"适用于巴黎和#34; mapfr"适用于法国,我已经删除了我在没有工作的情况下的尝试)

function CreateMarker(marker, map, infowindow, description)
    {
        google.maps.event.addListener(marker, 'click');
        marker.setMap(map);
    }



function initialize(ListeDesPoints) {
    var mapidfOptions = {
          center: { lat: 48.856614, lng: 2.352222},
          zoom: 11
        };
    var mapfrOptions = {
          center: { lat: 46.227638, lng: 2.213749},
          zoom: 5
        };

     var mapidf = new google.maps.Map(document.getElementById('maps-idf'),
            mapidfOptions);

     var mapfr = new google.maps.Map(document.getElementById('maps-fr'),
            mapfrOptions);


    var infowindow = new Array();
    var marker = new Array();

    for (var i = 0; i < ListeDesPoints.length; i++)
        {

                marker[i] = new google.maps.Marker({
                                        position: new google.maps.LatLng (ListeDesPoints[i]['lat'], ListeDesPoints[i]['lng']),
                                        title: ListeDesPoints[i]['address']
                                 });
                var max_lat = 48.9602260;
                var min_lat = 48.7451930;
                var max_lng = 2.6327379;
                var min_lng = 2.0946256;
                        if(ListeDesPoints[i]['lat'] > max_lat || ListeDesPoints[i]['lat'] < min_lat || ListeDesPoints[i]['lng'] > max_lng || ListeDesPoints[i]['lng'] < min_lng)
                            {
                                var map = mapfr;
                            }
                        else
                            {
                                var map = mapidf;
                            }
                infowindow[i] = new google.maps.InfoWindow({content:""})
                var description = '<h2>'+ ListeDesPoints[i]['address']+'</h2><img src="'+ListeDesPoints[i]['image']+'" />';


                CreateMarker(marker[i], map, infowindow[i], description);
        }


}

你有可能注意到我做错的其他事情......请随意批评这段代码:)

非常感谢

2 个答案:

答案 0 :(得分:0)

最简单的方法可能是创建单一信息窗口实例并将其重用于所有标记,如下所示:

var infowindow = new google.maps.InfoWindow({});
var marker = new Array();

for (var i = 0; i < data.length; i++) {

      marker[i] = new google.maps.Marker({
           position: new google.maps.LatLng(data[i]['lat'], data[i]['lng']),
           title: data[i]['address'],
           map: map
      });

      google.maps.event.addListener(marker, 'click', function () {
          infowindow.setContent('Some content goes here');
          infowindow.open(map);
      });

}

根据Info Windows

  

最佳做法:为获得最佳用户体验,只需一个信息窗口   应该在任何时候在地图上打开。多个信息窗口   地图显得杂乱无章。如果您一次只需要一个信息窗口,   您可以只创建一个InfoWindow对象并在不同的位置打开它   地图事件上的位置或标记,例如用户点击。如果你这样做   需要多个信息窗口,可以显示多个InfoWindow   对象同时出现。

在这种情况下,当您点击新标记而不必致电close() method时,信息窗口应自动关闭。

JSFiddle:Modified example

答案 1 :(得分:0)

如果您希望每个地图都有一个标记,请为每个地图创建一个单独的InfoWindow,并将其与标记相关联,就像您执行地图一样。

for (var i = 0; i < ListeDesPoints.length; i++) {

    marker[i] = new google.maps.Marker({
        position: new google.maps.LatLng(ListeDesPoints[i]['lat'], ListeDesPoints[i]['lng']),
        title: ListeDesPoints[i]['address']
    });
    var max_lat = 48.9602260;
    var min_lat = 48.7451930;
    var max_lng = 2.6327379;
    var min_lng = 2.0946256;
    if (ListeDesPoints[i]['lat'] > max_lat || ListeDesPoints[i]['lat'] < min_lat || ListeDesPoints[i]['lng'] > max_lng || ListeDesPoints[i]['lng'] < min_lng) {
        var map = mapfr;
        var infowindow = infowindowfr;
    } else {
        var map = mapidf;
        var infowindow = infowindowidf;
    }
    //infowindow[i] = new google.maps.InfoWindow({ content: "" });
    var description = '<h2>' + ListeDesPoints[i]['address'] + '</h2><img src="' + ListeDesPoints[i]['image'] + '" />';


    CreateMarker(marker[i], map, infowindow, description);
}

proof of concept fiddle

代码段

function CreateMarker(marker, map, infowindow, description) {
  marker.setMap(map);

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(description);
    infowindow.open(map, this);
  });
}

function initialize(ListeDesPoints) {
  var mapidfOptions = {
    center: {
      lat: 48.856614,
      lng: 2.352222
    },
    zoom: 11
  };
  var mapfrOptions = {
    center: {
      lat: 46.227638,
      lng: 2.213749
    },
    zoom: 5
  };

  var mapidf = new google.maps.Map(document.getElementById('maps-idf'),
    mapidfOptions);

  var mapfr = new google.maps.Map(document.getElementById('maps-fr'),
    mapfrOptions);

  //var infowindow = new Array();
  var infowindowfr = new google.maps.InfoWindow({});
  var infowindowidf = new google.maps.InfoWindow({});

  var marker = new Array();

  for (var i = 0; i < ListeDesPoints.length; i++) {

    marker[i] = new google.maps.Marker({
      position: new google.maps.LatLng(ListeDesPoints[i]['lat'], ListeDesPoints[i]['lng']),
      title: ListeDesPoints[i]['address']
    });
    var max_lat = 48.9602260;
    var min_lat = 48.7451930;
    var max_lng = 2.6327379;
    var min_lng = 2.0946256;
    if (ListeDesPoints[i]['lat'] > max_lat || ListeDesPoints[i]['lat'] < min_lat || ListeDesPoints[i]['lng'] > max_lng || ListeDesPoints[i]['lng'] < min_lng) {
      var map = mapfr;
      var infowindow = infowindowfr;
    } else {
      var map = mapidf;
      var infowindow = infowindowidf;
    }
    //infowindow[i] = new google.maps.InfoWindow({ content: "" });
    var description = '<h2>' + ListeDesPoints[i]['address'] + '</h2><img src="' + ListeDesPoints[i]['image'] + '" />';


    CreateMarker(marker[i], map, infowindow, description);
  }

}

//Usage
google.maps.event.addDomListener(window, 'load', function() {
  //1.load data
  var points = [{
    'lat': 48.856614,
    'lng': 2.352222,
    'address': 'Paris',
    'image':'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5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kRNIUE5jOM4+1QW9nKxintpSrRMGjMi7qcBgR9am8K6Kbsux99csnDr14mAMcgIwdwxbY/hRqSoJlHLkeR1l0j4Q3h6Z86rmgntLS5V1URy7ySDBOwPc/Oo0u4UjXR4+4/ZoPC2qCs1M1xkEduxTlodIxpXLfCB36b0PPxq2t2KXdyquh8KSnUy9+n76Cs7PxK6ubc2/OeFGGC0J0tj51TtwYLJzHuDIh3Kuu7H1IO9Sjpkh56hvg0V5xC/vm18OS2mt025kkytvjyBOD7Gu1X2rR20HLjUAd2G2d6VdCxUR3L7Lh0YMApHMBYDPXKtj75H386kmjS5iWRVGdOpPM57UqVMZoEN7bInLMuskbKoySP16fWgG4zKU028SRY21Yyf0pUqokiLbAJpZJm1Suzn/cc1CGrtKnFFG7q2etEyzkxjJ7UqVLQUwUzHt0pUqVOKf//Z'}];
  
  initialize(points);
});
#maps-fr,
#maps-idf {
  height: 240px;
  width: 640px;
  margin: 0px;
  padding: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="maps-fr"></div>
<div id="maps-idf"></div>