【问题标题】:Pan and center google map marker on click点击时平移和居中谷歌地图标记
【发布时间】:2016-12-28 18:23:57
【问题描述】:

我正在使用 Google 地图 API 来显示多个工作正常的位置。我添加了链接,当点击相应的链接时,我想用它来居中位置。

我已将 onclick 添加到该位置的中心,但它不起作用

<a id="1" href="#">Go to 1</a><br>
<a id="2" href="#">Go to 2</a><br>
<a id="3" href="#">Go to 3</a><br>
<a id="4" href="#">Go to 4</a><br>
<a id="5" href="#">Go to 5</a><br>

var labels = '123456';
var labelIndex = 0;

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(-33.92, 151.25),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

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,
    label: labels[labelIndex++ % labels.length],

  });
};

$("a").click(function(){
//zoom in on location

  var id = ($(this).data('id'));
   google.maps.event.trigger(id , "resize");
   map.panTo(marker.getPosition());
   map.setZoom(14);
  return false;
});

Project on Jsfiddle

【问题讨论】:

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


    【解决方案1】:

    更新小提琴:http://jsfiddle.net/3gz43tqq/5/

    工作代码 sn-p:

    var labels = '123456';
    var labelIndex = 0;
    
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];
    
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    
    var infowindow = new google.maps.InfoWindow();
    
    var marker, i;
    var markers = [];
    for (i = 0; i < locations.length; i++) {
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        label: labels[labelIndex++ % labels.length],
      });
      markers.push(marker);
    };
    
    $("a").click(function() {
      // labels start at 1, array starts at 0
      map.panTo(markers[this.id-1].getPosition());
      map.setZoom(14);
      return false;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.google.com/maps/api/js"></script>
    <div id="map" style="width: 500px; height: 400px;"></div>
    <div>
      <a id="1" href="#">Go to 1</a>
      <br>
      <a id="2" href="#">Go to 2</a>
      <br>
      <a id="3" href="#">Go to 3</a>
      <br>
      <a id="4" href="#">Go to 4</a>
      <br>
      <a id="5" href="#">Go to 5</a>
      <br>
    </div>

    【讨论】:

      【解决方案2】:

      我已经在 J​​SFiddle 中更新了你的代码:http://jsfiddle.net/3gz43tqq/3/

      $("a").click(function(){
          var id = this.id;
          map.setCenter(new google.maps.LatLng(locations[id][1], locations[id][2]))
          return false;
      });
      

      您可以从 a 使用函数 setCenter(LatLng object)

      获取 id

      【讨论】:

        猜你喜欢
        • 2012-09-27
        • 1970-01-01
        • 2012-01-31
        • 2012-12-04
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        • 2012-09-05
        • 1970-01-01
        相关资源
        最近更新 更多