GLatLngBounds - 错误的中心和缩放级别

时间:2009-06-02 11:54:33

标签: javascript google-maps

我正在尝试使用GLatLngBounds来使地图上的所有标记都可见。以下是我正在做的一个小例子。

INV.createMap = function(containerId) {
    var map = null;
    var geocoder = null;
    var bounds = new GLatLngBounds();

    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(containerId), {
            size: new GSize(600, 300)
        });
        map.setCenter(new GLatLng(54.729378425601766, 25.279541015625), 15);
        map.addControl(new GSmallZoomControl());
        geocoder = new GClientGeocoder();
    }

    return {
        markAdress: function(address, infoContentHtml) {
            if (map !== null && geocoder !== null) {
                geocoder.getLatLng(address, function(point) {
                    if (point) {
                        var marker = new GMarker(point);
                        GEvent.addListener(marker, 'mouseover', function() {
                            if (!map.getInfoWindow().getPoint().equals(this.getLatLng())) {
                                this.openInfoWindowHtml(infoContentHtml);
                            }
                        });
                        map.addOverlay(marker);
                        bounds.extend(point);
                    }
                });
            }
        },

        finalize: function() {
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        }
    };
};

用法:

var m = INV.createMap('whatever');
var addresses = ...
for (var i = 0, l = addresses.length; i < l; i++) {
    m.markAdress('address...', 'htmlInfo...');
}
m.finalize();

问题是缩放级别是完全错误的(waaay太大了),并且由于某种原因,标记出现在地图的左上角(但所有这些都是可见的)。

我做错了什么?

编辑:忽略这个问题。我犯了一个愚蠢的错误 - 我忽略了GClientGeocoder发出异步请求的事实,因此太早调用了finalize()方法。

1 个答案:

答案 0 :(得分:1)

下面函数调用的最后一个参数指定缩放级别:

map.setCenter(new GLatLng(54.729378425601766, 25.279541015625), 15);

This article谈论缩放。因此缩放级别有点太高了。

进一步this tutorial告诉您如何适应地图缩放以正确显示一组标记。

希望这有帮助

相关问题