响应式 Google Maps API v3 - 在小屏幕上自动显示所有标记

烛台

我正在设计一个包含Google Maps APIFull Screen 中的小网络应用程序webapp 旨在成功地用于移动设备(甚至是小型设备)。实际上我从我的Database使用中获取一些坐标AJAX并将它们放在地图中。

locations使用 AJAX推送数组中的坐标,然后执行以下操作:

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 11,
  center: new google.maps.LatLng(37.262577, -115.8314989),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControlOptions: {
    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
    position: google.maps.ControlPosition.BOTTOM_LEFT
  }
});

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

var marker, i;
for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    //locations[i][1] is lang
    //locations[i][2] is lng
    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));
}

如何以响应方式转换此脚本?我想在屏幕上看到我的所有标记,移动屏幕 <= 768px(宽度),我找不到任何通用处理程序来巧妙地调整(放大/缩小)地图的大小以查看屏幕中的所有标记。

谢谢阅读!

魔法师

最好的办法是制作一个LatLngBounds对象并使用您想要显示的每对坐标对其进行扩展。然后,只需在您的地图对象上调用fitBounds(),并将您的边界作为参数,如下所示:

var marker, i;
var bounds = new google.maps.LatLngBounds()
for (i = 0; i < locations.length; i++) {  
  var coords =  new google.maps.LatLng(parseFloat(locations[i][1]), parseFloat(locations[i][2]))
  marker = new google.maps.Marker({
    //locations[i][1] is lang
    //locations[i][2] is lng
    position: coords,
    map: map
  });
  
  bounds.extend(coords);

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

map.fitBounds(bounds);

如果您只想在需要时应用,那么您必须通过比较它们的四肢 :getNorthEast()getSouthWest()坐标来比较您使用 map.getBounds() 创建的变量边界

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Google Maps API v3中使用多个标记自动对中地图

关闭Google Maps API v3中的所有信息窗口

Google Maps API v3将InfoWindow添加到每个标记

如何在Google Maps API v3中使用SVG标记

Chrome自动填充功能涵盖了Google Maps API v3的自动完成功能

Google Maps API v3,“删除标记”图标或更改为“空”?

Google Maps v3 API错误

Google Maps API 3-在屏幕上显示所有标记,但保留中心点

Google Maps API v3 –使用复选框过滤标记(每个标记多个标记)

如何在Android的Google Maps API v3中更改标记大小?

动画化不同的标记API V3 Google Maps

如何创建直接链接以访问特定的Google Maps API V3标记?

Google Maps v3未显示

Google Maps Javascript API V3:标记具有相同的标题

google maps api v3在可拖动标记之间绘制折线

标记拖动事件Google Maps API V3

标记位置未获取-Google Maps API v3

Google Maps JavaScript API v3更改标记并添加卫星视图

带有jQuery自动完成插件的Google Maps Api v3位置搜索

Google Maps API v3标记问题

Google Maps API v3唯一标记JS

地图标记无法呈现-Phonegap中的Google Maps Javascript API v3

Google Maps V3样式或标记未显示,具体取决于我使用的API JavaScript JavaScript源

Google Maps V3样式或标记未显示,具体取决于我使用的API JavaScript JavaScript源

Google Maps Javascript API v3的标记

Google Maps JS api v3延迟拖拽标记

Google Maps Javascript API V3中的旋转标记

带有信息窗口和图钉的 Google Maps API v3 多个标记

Google Maps v3 API - 自动平滑放大到标记