GLatLngBounds - wrong center and scale level

I am trying to use GLatLngBounds to make all the markers on the map visible. Below is a small example of what I am doing.

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));
        }
    };
};

Using:

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

The problem is that the zoom level is completely wrong (waaay is too high), and for some reason, the markers appear in the upper left corner of the map (but they are all visible).

What am I doing wrong?

EDIT: ignore this question. I made a stupid mistake - I overlooked the fact that GClientGeocoder makes asynchronous requests, so the finalize () method is called too early.

+3
source share
1 answer

:

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

. , .

, , .

,

+1

Source: https://habr.com/ru/post/1709591/


All Articles