单击Infowindow后重置Google Map位置

时间:2017-06-20 20:41:34

标签: google-maps

我有一个谷歌地图,其中包括点击地图中的标记时的信息窗口。单击标记时,该标记的信息窗口将打开并重新定位地图。

客户希望地图位置重置为原始位置,并在点击地图中关闭信息窗的任何位置时关闭信息窗。

我到目前为止的代码如下。谢谢!!

  <script src="https://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
var locations = [
  ['<div class="leftalign"><?php echo $addr1; ?></div> <div class="rightalign"><img src="<?php echo $addr1_image; ?>" alt="General Image"></div>', 38.4505064,-122.7268983, 4],
  ['<div class="leftalign"><?php echo $addr2; ?></div> <div class="rightalign"><img src="<?php echo $addr2_image; ?>" alt="General Image"></div>', 38.377517, -121.946924, 5],
  ['<div class="leftalign"><?php echo $addr3; ?></div> <div class="rightalign"><img src="<?php echo $addr3_image; ?>" alt="General Image"></div>', 38.574165, -121.559870, 3],
  ['<div class="leftalign"><?php echo $addr4; ?></div> <div class="rightalign"><img src="<?php echo $addr4_image; ?>" alt="General Image"></div>', 38.794887, -121.217039, 2],
  ['<div class="leftalign"><?php echo $addr5; ?></div> <div class="rightalign"><img src="<?php echo $addr5_image; ?>" alt="General Image"></div>', 38.670913, -121.160873, 1]
];

var map = new google.maps.Map(document.getElementById('custommaploading'), {
  zoom: 9,
  center: new google.maps.LatLng(38.477191,-121.5400331),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  scrollwheel: false,
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

1 个答案:

答案 0 :(得分:-1)

我明白了。以下是其他人需要的答案:

//Latitude and Longitude position
var myLatlng = {lat: 38.477191, lng: -121.5400331};

//On map click
google.maps.event.addListener(map, "click", function(event) {
//Close all info windows
infowindow.close();
//Center the map
map.setCenter(myLatlng);
});