完全缩小后如何使用谷歌地图获得真正的界限

时间:2010-03-01 22:42:56

标签: google-maps

我有一张地图,根据地图的gbounds显示位置点。例如,无论何时移动/缩放地图,我都会找到属于这些边界内的位置的边界和查询。不幸的是,当完全缩小时,我无法显示所有位置。原因是,gmaps将最小/最大长度报告为地图边缘的任何内容,但如果缩小到足够大,则可以获得排除可见位置的纵向范围。

例如,如果您缩放地图以便在最左侧和最右侧看到两次NorthAmerica。最小/最大长度约为:-36.5625至170.15625。但这几乎完全排除了位于-180到-60范围内的NorthAmerica。显然这很麻烦,因为你实际上可以看到大陆NorthAmerica(两次),但当我查询我在谷歌地图范围内的位置时,不会返回NorthAmerica。

我找到min / max long的代码是:

bounds = gmap.getBounds();
min_lat = bounds.getSouthWest().lat()
max_lat = bounds.getNorthEast().lat()

是否有人遇到此问题,有人可以建议解决方法吗?在我的头脑中,我只能做一个黑客攻击:检查缩放级别并在必要时将最小/最大拉特硬编码为-180/180,这绝对是不可接受的。

3 个答案:

答案 0 :(得分:0)

这不是最优雅的解决方案,但我测试了它,它确实有效。检查当前边界是否包含完整地图的边界。谷歌实际上并没有包括北极和南极,因此我不得不将其调整为几乎一直向南和向南。

bounds = gmap.getBounds();
var fullEarth = new GLatLngBounds(new GLatLng(-85, 180), new GLatLng(85, -180)))
var isFull = bounds.containsBounds(fullEarth);
min_lat = isFull ? -90 : bounds.getSouthWest().lat()
max_lat = isFull ? 90 : bounds.getNorthEast().lat()

min_lng = isFull ? 180 : bounds.getSouthWest().lng()
max_lng = isFull ? -180 : bounds.getNorthEast().lng()

答案 1 :(得分:0)

甚至无法想象这个巨大的虫子是如何存在的!!! isFullLng和toSpan函数都是错误的,你不能依赖它们 我的错误解决方法:

var zoom = map.getZoom();
var bounds = map.getBounds();    
var span = bounds.toSpan();
var isFull = span.lng() > 277 || zoom < 2;

答案 2 :(得分:0)

这是我的解决方案,我只需要isFullLng(),它显然是从API v3中删除的:

isFullLng: function() {
        var scale = Math.pow(2, map.getZoom()),
            bounds = map.getBounds(),
            ne = bounds.getNorthEast(),
            sw = bounds.getSouthWest(),
            lat = (ne.lat() <= 0 && sw.lat() >= 0) || (ne.lat() >= 0 && sw.lat() <= 0) ? 0 : Math.min(Math.abs(ne.lat()), Math.abs(sw.lat())), // closest latitude to equator
            deg1 = new google.maps.LatLng(lat, 0),
            deg2 = new google.maps.LatLng(lat, 1),
            coord1 = map.getProjection().fromLatLngToPoint(deg1),
            coord2 = map.getProjection().fromLatLngToPoint(deg2);
        // distance for one long degree in pixels for this zoom level
        var pixelsPerLonDegree = (coord2.x - coord1.x) * scale;
        // width of map's holder should be <= 360 (deg) * pixelsPerLonDegree if full map is displayed
        var width = $this.width(); // width of map's holder div
        return pixelsPerLonDegree * 360 <= width;

    },
isFullLat: function() {
        var bounds = map.getBounds(),
            ne = bounds.getNorthEast(),
            sw = bounds.getSouthWest(),
            maxLat = 85; // max lat degree
        return ne.lat() >= maxLat && sw.lat() <= -maxLat;
    },

那么isFull可能是:

  

isFullLng()&amp;&amp; isFullLat()

请注意,在isFullLng()中需要map的占位符的宽度,我使用的是jQuery,map是在$ this引用的div中呈现的,所以我只调用

  

$ this.width()

您应该更改它以将其应用于您的问题。