点击侧边栏元素上的Google集群地图会打开特定的信息窗口

时间:2018-10-09 14:13:16

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

我试图通过单击侧边栏元素来实现带有侧边栏的Google集群地图,当选择特定的侧边栏元素时,它需要显示特定的信息窗口。我找到了一个教程,但是它正在使用外部API。但是我试图实现的是获得自定义值。我正在添加要在此处添加的代码。

<html>
<head>
<script type='text/javascript'  src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>
<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?key=AIzaSyBFLmEBCM9NFv4yDK6oXwDKX3FZjmvVqUk'></script>
<script type='text/javascript'  src='https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js'></script>
</head>
<body>



    <div style="width:50%;float:left;">
    <div id="map"></div>
    <div style="width:50%; float:left;">
      <a href="#" onclick="myClick(0);">Hello 1</a>
      <a href="#" onclick="myClick(1);">Hello 2</a>
      <a href="#" onclick="myClick(2);">Hello 3</a>
      <a href="#" onclick="myClick(2);">Hello 4</a>

    </div>
    </div>
    <script>
    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: {
          lat: 35.1264,
          lng: 33.4299
        }
      });
      var infoWin = new google.maps.InfoWindow();
      // Add some markers to the map.
      // Note: The code uses the JavaScript Array.prototype.map() method to
      // create an array of markers based on a given "locations" array.
      // The map() method here has nothing to do with the Google Maps API.
      var markers = locations.map(function(location, i) {
        var marker = new google.maps.Marker({
          position: location,
          icon:'https://www.tutorialspoint.com/google_maps/src/Picture1.png'
        });
        google.maps.event.addListener(marker, 'mouseover', function(evt) {
          infoWin.setContent(location.info);
          infoWin.open(map, marker);

        })
        return marker;
      });

      // markerCluster.setMarkers(markers);
      // Add a marker clusterer to manage the markers.
      var markerCluster = new MarkerClusterer(map, markers, {
        imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
      });

    }
    var locations = [   
    {
    lat: 35.1218939381583,
    lng: 33.42075903167722,
    info: " Hello 1"
    },

    {
    lat: 35.11596152729056,
    lng: 33.42702467193601,
    info: " Hello 2"
    },

    {
    lat: 35.116312569544064,
    lng: 33.43535024871824,
    info: " Hello 3"
    },

    {
    lat: 35.11497860093425,
    lng: 33.430372068786596,
    info: " Hello 4"
    },
    ];
    google.maps.event.addDomListener(window, "load", initMap);



    //on click function
      function myClick(id) {
        google.maps.event.trigger(markers[id], 'click');
      }
    </script>
    <style>
    #map {
      height: 950px;
    }
    .map-img{float:left;}
    .map-title{width:100%; float:left;}
    </style> 

    </body>
    </html>

Google集群工作正常,但单击侧边栏元素是因为它没有弹出信息窗口。

1 个答案:

答案 0 :(得分:2)

“群集”标记不会添加到地图中。在您的myClick函数中,将它们添加到地图中或放大,以便集群器“解簇”它们。

function myClick(id) {
  map.setCenter(markers[id].getPosition());
  map.setZoom(16);
  google.maps.event.trigger(markers[id], 'mouseover'); // to display InfoWindow
}

proof of concept fiddle

screenshot of resulting map after clicking "Hello 3"

代码段:

html,
body {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px;
}

#map {
  height: 100%;
  width: 50%;
  float: right;
}
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>
<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js'></script>
<script type='text/javascript' src='https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js'></script>
<div style="width:100%;height:100%;">
  <div id="map"></div>
  <div style="width:50%; float:left;">
    <a href="#" onclick="myClick(0);">Hello 1</a><br>
    <a href="#" onclick="myClick(1);">Hello 2</a><br>
    <a href="#" onclick="myClick(2);">Hello 3</a><br>
    <a href="#" onclick="myClick(3);">Hello 4</a><br>

  </div>
</div>
<script>
  var markers;
  var map;

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: {
        lat: 35.1264,
        lng: 33.4299
      }
    });
    var infoWin = new google.maps.InfoWindow();
    // Add some markers to the map.
    // Note: The code uses the JavaScript Array.prototype.map() method to
    // create an array of markers based on a given "locations" array.
    // The map() method here has nothing to do with the Google Maps API.
    markers = locations.map(function(location, i) {
      var marker = new google.maps.Marker({
        position: location,
        icon: 'https://www.tutorialspoint.com/google_maps/src/Picture1.png'
      });
      google.maps.event.addListener(marker, 'mouseover', function(evt) {
        infoWin.setContent(location.info);
        infoWin.open(map, marker);

      })
      return marker;
    });

    // markerCluster.setMarkers(markers);
    // Add a marker clusterer to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers, {
      imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
    });
  }
  var locations = [{
      lat: 35.1218939381583,
      lng: 33.42075903167722,
      info: " Hello 1"
    },

    {
      lat: 35.11596152729056,
      lng: 33.42702467193601,
      info: " Hello 2"
    },

    {
      lat: 35.116312569544064,
      lng: 33.43535024871824,
      info: " Hello 3"
    },

    {
      lat: 35.11497860093425,
      lng: 33.430372068786596,
      info: " Hello 4"
    },
  ];
  google.maps.event.addDomListener(window, "load", initMap);



  //on click function
  function myClick(id) {
    map.setCenter(markers[id].getPosition());
    map.setZoom(16);
    google.maps.event.trigger(markers[id], 'mouseover');
  }
</script>