Google地图以最小缩放范围进行绑定 [英] Google Maps bounds on minimal zoom

查看:82
本文介绍了Google地图以最小缩放范围进行绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Google地图填充整个页面。
以下是一个示例:

I have a simple Google Map that fill the whole page. Here is a sample:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Marker Animations</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var stockholm = new google.maps.LatLng(59.32522, 18.07002);
  var map;

  function initialize() {
    var mapOptions = {
      zoom: 0,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: stockholm
    };

    map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);

   google.maps.event.addListener(map, 'idle', function() {
      var b = map.getBounds();
      document.getElementById("bounds").innerText = b.getSouthWest().lng() + ", " + b.getNorthEast().lng();
   });

  }
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 100%; height: 100%;">map div</div>
<div id="bounds" style="left: 100px; top: 0px; width: 300px; height: 30px; position: absolute; background-color: #a0a0a0;"></div>
</body>
</html>

在MINIMUM缩放级别,当您可以看到多个世界地图时,我预计经度界限为-180 - 180,但实际上我不明白。我的期望错了,为什么?

At MINIMUM zoom level, when you can see several world maps, I expect longitude bounds to be -180 - 180, but in real I don't get it. Is my expectation wrong and why?

在实际应用程序中,我有一个服务器端代码,根据当前地图边界从数据库中提取标记。 b

In real application I have a server-side code that fetches markers from database depending on current map bounds.

推荐答案

边界将在窗口边界报告lat和lng - 即使地图已经包装了几次。所以,窗口右下角的lat,lng会是一个点,左上角是另一个点。

The bounds will report the lat and lng at the bounds of the window - even if the map has wrapped a few times. So what ever the lat,lng of the bottom right of your window will be one point, and the upper left will be the other.

我有同样的问题,我最终做的是创建一个90,180到-90,-180的世界范围,然后看看它是否适合

I had this same issue and what I ended up doing was creating a World bounds that was 90, 180 to -90, -180 and then seeing if that fit in the window.

var ne = new google.maps.LatLng(90, 180);
var sw = new google.maps.LatLng(-90, -180)
var world = new google.maps.LatLngBounds(ne, sw);
var fetchbounds;
if(map.getBounds().contains(ne) && map.getBounds().contains(sw)){
    fetchBounds = world;
}else{
    fetchBounds = map.getBounds();
}

这篇关于Google地图以最小缩放范围进行绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆