1
我正在将我的网站从Google地图转换为单张/ openstreetmap。在网站上,搜索结果在地图上显示为标记。结果可以有一个标记,或10或200.这一切都很好,很好。但是当结果有多个标记时,我无法将它缩放/放大。相反,它只是放大一个(可能是因为我把map.setView放大到18!)。缩放以适合leaflet.js标记
在我的情况下,是否有替代使用map.setView?我不想在所有情况下将缩放比例设置为18。但希望缩放只适合内容。我知道这里有一些类似的问题,但是他们没有帮助,因为我不知道如何用map.setView替换不能对变焦进行硬编码的东西。这是我的全部功能:
function showLocations(ids, lats, lons, contents) {
locationIDs = ids;
infoWindows = new Array();
markers = new Array();
map = new L.Map('map_canvas');
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 1, maxZoom: 19, attribution: osmAttrib});
for (i in ids) {
var infoWindow = L.popup()
.setContent(contents[i]);
map.setView(new L.LatLng(lats[i], lons[i]),18);
map.addLayer(osm);
L.marker([lats[i], lons[i]]).addTo(map)
.bindPopup(infoWindow)
.openPopup();
}
}
与谷歌地图,我用这个:
markers.push(marker);
bounds.extend(latlng);
if (contents.length === 1) {
map.setZoom(18);
map.setCenter(new google.maps.LatLng(lats[0], lons[0]));
} else {
map.fitBounds(bounds);
}
谢谢!
谢谢了,亚历克斯!我还没有插入它,但这看起来很棒。我很欣赏额外的提示。在阅读教程/例子时,我有时会遇到一个问题,那就是它们并不总是显示完整的功能(因为每种情况都不一样),而fitBounds和LatLngBounds这样的东西最终不会与我的其他东西玩得很好。我还没有足够的技巧来解决所有问题,尽管我尝试了! – Ganberry
当我得到leaflet.js错误时,你知道这意味着什么吗?如“TypeError:o.DomUtil.getPosition(...)未定义”或“TypeError:this._startPos未定义”或“TypeError:this._northEast未定义”。我正在使用cdn中的leaflet-0.7.3。 – Ganberry
设置一个例子来说明你的问题,否则无法猜测。 –