MarkerLayer准备好不被调用边界功能

时间:2014-02-13 14:00:35

标签: javascript angularjs mapbox

我正在尝试使用边界功能使我的引脚适合屏幕。使用MapBox网站上的示例代码,我想出了这个。

    var map = L.mapbox.map('map', 'username.map-3li14vzt').setView([51.53769, -0.07655], 13);

    map.scrollWheelZoom.disable();
    map.doubleClickZoom.disable();

    var markerLayer = L.mapbox.markerLayer().setGeoJSON({
        type: 'FeatureCollection',
        features: $scope.geo // this is my json
    }).addTo(map);

    /* fit pins to screen */

    markerLayer.on('ready', function(e) {
        console.log('Maps ready!');
        map.fitBounds(markerLayer.getBounds());
    });

正确创建地图,并将针脚放在正确的位置。但是,未调用markerLayer就绪函数(在控制台中看不到“Maps ready!”)。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

由于您正在调用.setGeoJSON()ready事件不会触发 - 它仅适用于.loadURL()之类的异步调用。您可以完全删除该事件绑定并说:

var map = L.mapbox.map('map', 'username.map-3li14vzt').setView([51.53769, -0.07655], 13);

map.scrollWheelZoom.disable();
map.doubleClickZoom.disable();

var markerLayer = L.mapbox.markerLayer().setGeoJSON({
    type: 'FeatureCollection',
    features: $scope.geo // this is my json
}).addTo(map);

/* fit pins to screen */
console.log('Maps ready!');
map.fitBounds(markerLayer.getBounds());