Google Maps API:平移列表中的标记。

Rob*_*apa 5 javascript maps list panning

您好:)请多多包涵,我不是编码员,但我想从中学到东西。这是我目前正在处理的页面;http://www.websu.it/devnw/dev/contact.html。我目前已使用以下Javascript使用Google Maps API设置了地图:

    function initialize() {

          var mapOptions = {
          zoom: 5,
          center: new google.maps.LatLng(48.160, -6.832),
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP
          };

         map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
             setMarkers(map, cities);
              }


 var cities = [
   ['Groningen', 53.216723950863425, 6.560211181640625, 4],
   ['San Francisco', 34.01131647557699, -118.25599389648437, 5],
   ['New York City', 40.7143528, -74.0059731, 3],
   ['Amsterdam', 52.3702157, 4.8951679, 2],
   ['Maroubra Beach', -33.950198, 151.259302, 1]
 ];

 function setMarkers(map, locations) {

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

 This continues below...
Run Code Online (Sandbox Code Playgroud)

然后,我创建了一个列表,每个li元素在单击时都会导致将地图平移到这些标记之一。

我添加了以下代码,并且效果很好。但这意味着我必须在上面的“城市”代码数组以及下面的var laLatLng代码中为每个城市添加经度/纬度。如何在下面的平移脚本中更轻松地获得经度和纬度?

      $(document).ready(function() {
      initialize();

      $("#contacts").on("click", "#sanfransisco", function() {
          var laLatLng = new google.maps.LatLng(34.01131647557699, -118.25599389648437);
          map.panTo(laLatLng);
          map.setZoom(5);
       });
      $("#contacts").on("click", "#groningen", function() {
          var laLatLng = new google.maps.LatLng(53.216723950863425, 6.560211181640625);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#nyc", function() {
          var laLatLng = new google.maps.LatLng(40.7143528, -74.0059731);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#losangeles", function() {
          var laLatLng = new google.maps.LatLng(52.3702157, 4.8951679);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
  });
Run Code Online (Sandbox Code Playgroud)

我希望有人可以向我解释如何将城市中的变量转换为可点击列表JavaScript。非常感谢,非常感谢您的回复!

cha*_*tfl 5

如果将cities数组转换为类似这样的对象:

var cities = {
  'Groningen':  [ 53.216723950863425, 6.560211181640625, 4],
   'San Francisco': [ 34.01131647557699, -118.25599389648437, 5],
   'New York City': [ 40.7143528, -74.0059731, 3]

};
Run Code Online (Sandbox Code Playgroud)

data-在每个链接中添加一个具有匹配城市名称的属性以及一个通用的类名称:

/* not sure what html looks like , using pseudo "tagName"*/
<tagName class="map_link" data-city="Groningen">Groningen</tagName>
Run Code Online (Sandbox Code Playgroud)

您可以为整个类创建一个处理程序:

$("#contacts").on("click", ".map_link", function() {
          /* "this" within handler is the actual element clciked*/

          /* find correct array from "cities" object */
         var data=cities[ $(this).data('city') ]; /* returns array[ 53.21, 6.56, 4]*/
          var laLatLng = new google.maps.LatLng( data[0],  data[1]);
          map.panTo(laLatLng);
          map.setZoom( data[2]);
       });
Run Code Online (Sandbox Code Playgroud)


ade*_*neo 5

这是我的方法,只需使用 Google Maps 的反向地理编码功能来查找城市名称:

<ul id="cities">
  <li>San Fransisco</li>
  <li>Groningen</li>
  <li>New York City</li>
  <li>Los Angeles</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

JS

$('#cities li').on('click', function() {
  $.ajax({
    url: 'http://maps.googleapis.com/maps/api/geocode/json?address='+decodeURIComponent( $(this).text() )+'&sensor=false',
    type: 'GET'
  }).done(function(data) {
    var laLatLng = new google.maps.LatLng(data.results[0].geometry.location.lat, data.results[0].geometry.location.lng);
    map.panTo(laLatLng);
    map.setZoom(5);
  });
});
Run Code Online (Sandbox Code Playgroud)

小提琴