谷歌地图infowindow onclick

时间:2013-12-03 11:35:36

标签: javascript google-maps

我尝试使用infoWindow显示多个标记以显示位置信息。

如果infoWindow与 getMarkers 中的mk相关联,则地图会显示所有位置的信息,但位置错误。

如何让它们显示在正确的位置或只是没有显示并等待点击事件启动。

function getMarkers() {
      var latlngArr = [{ "FullSizeImageUrl": "/content/images/thumbs/0000415.jpeg", "ImageUrl": "/content/images/thumbs/0000415_200.jpeg", "Id": 3, "Address": "162 Hai Bà Trưng, P. Đa Kao, Q.1, TP Hồ Chí Minh", "Name": "Office \u0026 Showroom ", "LatLng": "(10.784913331461954, 106.6957104108368)", "Url": "/vn/shop/13/sabina-vietnam/3/office-showroom" }, { "FullSizeImageUrl": "/content/images/thumbs/0000415.jpeg", "ImageUrl": "/content/images/thumbs/0000415_200.jpeg", "Id": 4, "Address": "30 Nguyễn Công Hoan, Ngọc Khánh, Hà Nội, Vietnam", "Name": "SABINA", "LatLng": "(21.0272537, 105.81366600000001)", "Url": "/vn/shop/13/sabina-vietnam/4/sabina" }];

                $.each(latlngArr, function (index, data) {
                    var input = data.LatLng;
                    var latlngStr = input.replace('(', '').replace(')', '').split(",", 2);
                    var lat = parseFloat(latlngStr[0]);
                    var lng = parseFloat(latlngStr[1]);
                    var position = new google.maps.LatLng(lat, lng);

                    var mk = new google.maps.Marker({
                        position: position,
                        map: map,
                        id: data.Id,
                        title: data.Name,
                        infoWindow: createInfoWindow(data)   
                    });
                    markers.push(mk);
                    google.maps.event.addListener(mk, 'click', markerClick);
                });
         }     
              function createInfoWindow(info) {
                    return new google.maps.InfoWindow({
                        content:
                            '<div class="infoWindow">' +
                                '<a href="' + info.Url + '">' +
                                    '<div class="title">' +
                                        info.Name + "<br/>" + info.Address +
                                    '</div>' +
                                    '<div class="shopImg">' +
                                        '<img src="' + info.FullSizeImageUrl + '" alt="' + info.Name + '"/>' +
                                    '</div>' +
                                '</a>' +

                            '</div>',
                        map: map
                    });
                }
 function markerClick(e) {
        this.infoWindow.open(map, this);
    }

更新:我终于解决了它:

function getMarkers() {
            var latlngArr = [...];
            $.each(latlngArr, function (index, data) {
                var input = data.LatLng;
                var latlngStr = input.replace('(', '').replace(')', '').split(",", 2);
                var lat = parseFloat(latlngStr[0]);
                var lng = parseFloat(latlngStr[1]);
                var position = new google.maps.LatLng(lat, lng);

                var mk = new google.maps.Marker({
                    position: position,
                    map: map,
                    id: data.Id,
                    title: data.Name

                });

                markers.push(mk);               

                google.maps.event.addListener(mk, 'click', function () {
                    if (currentInfoWindow != '') {
                        currentInfoWindow.close();   // show one window at a time
                        currentInfoWindow = '';
                    }
                    var infoWindow = new google.maps.InfoWindow({ content:       createInfoWindow(data) });                    
                    infoWindow.open(map, mk);
                    currentInfoWindow = infoWindow;
                });
            });
        }
function createInfoWindow(info) {
        return '<div class="infoWindow">' +
                    '<a href="' + info.Url + '">' +
                        '<div class="title">' +
                            info.Name + "<br/>" + info.Address +
                        '</div>' +
                        '<div class="shopImg">' +
                            '<img src="' + info.FullSizeImageUrl + '" alt="' + info.Name + '"/>' +
                        '</div>' +
                    '</a>' + '</div>';
    }

1 个答案:

答案 0 :(得分:0)

<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>

function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
      '<div id="bodyContent">'+
      '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
      'sandstone rock formation in the southern part of the '+
      'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
      'south west of the nearest large town, Alice Springs; 450&#160;km '+
      '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
      'features of the Uluru - Kata Tjuta National Park. Uluru is '+
      'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
      'Aboriginal people of the area. It has many springs, waterholes, '+
      'rock caves and ancient paintings. Uluru is listed as a World '+
      'Heritage Site.</p>'+
      '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
      'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
      '(last visited June 22, 2009).</p>'+
      '</div>'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
      content: contentString
  });

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>
相关问题