【发布时间】:2016-04-07 23:11:27
【问题描述】:
为什么不能在 mapbox 和 googlemaps 之间切换底图,我在 console.log() 中收到此错误:
leaflet-src.js:1981 Uncaught Error: Map container is already initialized.
【问题讨论】:
标签: javascript google-maps leaflet mapbox
为什么不能在 mapbox 和 googlemaps 之间切换底图,我在 console.log() 中收到此错误:
leaflet-src.js:1981 Uncaught Error: Map container is already initialized.
【问题讨论】:
标签: javascript google-maps leaflet mapbox
添加
map.remove();
$('.basemap').append(' <div id="map"></div>');
【讨论】:
您可以使用map.remove method 销毁地图容器。但首先,您需要保留对地图的引用,以便以后参考。首次创建地图时,将其分配给var:
var MBmap = L.mapbox.map('map', 'pokaxperia.pk657nfi').setView([19.432711775616433, -99.13325428962708], 12);
然后,当您切换到 Google 时,您可以销毁 Mapbox 地图。将以下内容放入您的 _switchToGoogle 函数中:
if (MBmap) {
MBmap.remove();
MBmap = false;
}
并将您的 _switchToMapbox 函数更改为:
function _switchToMapbox() {
if (!MBmap) {
MBmap = L.mapbox.map('map', 'pokaxperia.pk657nfi').setView([19.432711775616433, -99.13325428962708], 12);
}
}
更新小提琴:
https://jsfiddle.net/tooy7dsk/2/
请记住,如果您只想切换背景图块(即您不需要街景等其他 Google 地图功能),则可以通过 Leaflet plugin 使用 Mapbox 中的 Google 图块。
【讨论】: