谷歌地图API V3 fitbounds()缩小但从未缩小

abe*_*key 9 jquery google-maps google-maps-api-3 fitbounds

我已经创建了一个非常复杂的商店定位器.用户输入他们的zip并且表格在地图上返回带有相应标记的结果.他们可以翻阅结果并且标记进一步移动,并且该fitbounds()功能非常适合缩小到适当的缩放级别以适合标记.问题在于,如果您回到较近的位置,fitbounds()则不会放大.即使您输入新的搜索,它也不会放大这些新标记 - 它以它们为中心,但保持在它所处的任何缩放级别.先前.我希望这是有道理的.如果有人知道我需要添加什么来让它放大更接近的结果请帮助.这些是我在标记推送功能结束时调用的谷歌功能:

  map.setCenter(bounds.getCenter());
  map.panToBounds(bounds);
  map.fitBounds(bounds);
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 10

问题是:我们设定

var bounds = new google.maps.LatLngBounds();
Run Code Online (Sandbox Code Playgroud)

以便我们以后可以将我们的标记放到地图上的有界区域.GMaps将始终异步缩小以适应fitBounds(),但不会放大以实现相同(如前所述@broady).这对于许多应用程序来说并不理想,因为一旦你离开并在地图上显示一系列标记导致地图缩小(可能<10),它就不会在没有用户手动这样做的情况下缩放.

GMaps将继续使用(缺少更好的单词)最大缩小标记收集状态(抱歉)的界限.在每次调用新标记之前设置为"null"会为您提供一个新的地图.

要自动放大,只需设置LatLngBounds(); 像这样'null'(参见下面的伪示例来查看它的位置):

bounds = new google.maps.LatLngBounds(null);
Run Code Online (Sandbox Code Playgroud)

伪示例:

// map stuff/initiation
...

var bounds = new google.maps.LatLngBounds();
var gmarkers = [];

function CreateMarker (obj) {
    myLatLng = new google.maps.LatLng(obj['latitude'], obj['longitude']);
    marker = new google.maps.Marker({
        position: myLatLng,
        map: map
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(obj['job']);
            infowindow.open(map, marker);
        }
    })(marker, i));
    bounds.extend(myLatLng);
    gmarkers.push(marker);
}

....

// here's an AJAX method I use to grab marker coords from a database:

$.ajax({
    beforeSend: function() {
        clear_markers(gmarkers); // see below for clear_markers() function declaration
    },
    cache: false,
    data: params,
    dataType: 'json',
    timeout: 0,
    type: 'POST',
    url: '/map/get_markers.php?_=<?php echo md5(session_id() . time() . mt_rand(1,9999)); ?>',
    success: function(data) {
        if (data) {
            if (data['count'] > 0) {
                var obj;
                var results = data['results'];

                // Plot the markers
                for (r in results) {
                    if (r < (data['count'])) {
                        CreateMarker(results[r]);
                    }
                }
            }
        }
    },
    complete: function() {
        map.fitBounds(bounds);
    }
});

// clear_markers()
function clear_markers(a) {
    if (a) {
        for (i in a) {
            a[i].setMap(null);
        }
        a.length = 0;
    }
    bounds = new google.maps.LatLngBounds(null); // this is where the magic happens; setting LatLngBounds to null resets the current bounds and allows the new call for zoom in/out to be made directly against the latest markers to be plotted on the map
}
Run Code Online (Sandbox Code Playgroud)


小智 9

这里没有什么花哨的东西.首先适合边界然后平移它.这将为您提供适当的缩放并包含整个边界.

map.fitBounds(bounds);
map.panToBounds(bounds);
Run Code Online (Sandbox Code Playgroud)

  • 这似乎有效.我想知道没有`fitBounds`应该做什么`panToBounds`. (2认同)

Chr*_*oot 6

没错,fitBounds确保提供的边界是可见的。它不会放大到适当的级别。

您可以先调用setZoom(20),然后调用fitBounds。


Gre*_*g W 0

map.setZoom(10);
Run Code Online (Sandbox Code Playgroud)

我认为可接受的范围是1-20。根据我的经验,只有整数,小数打破了地图。