javascript - 传单仅加载一个图 block

标签 javascript leaflet intel-xdk

我对 Leaflet 有一个问题,它实际上阻碍了我的整个工作。由于某些我无法解释的原因,Leaflet 的 UI 已正确加载到我的英特尔 XDK 应用程序中,但只加载了一个 map 图 block - 相同的代码在另一个测试应用程序中工作!现在,我已经尽力了,我希望这里有人能解决我的问题。

为了更好地理解,这是我的 leaflet.js 中的代码(它不是 leaflet.js,因为我使用 leaflet-src.js 作为脚本)和屏幕截图应用程序的 map 窗口。

function initLeaflet() {
document.getElementById("map").setAttribute("style", "height:" + window.innerHeight + "px; width:" + window.innerWidth + "px;");
var map = L.map('map');

L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery © <a href="http://mapbox.com">Mapbox</a>',
    id: 'examples.map-i875mjb7'
}).addTo(map);

map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);

map.locate({setView: true, maxZoom: 16});

map.on('click', onMapClick);
}

function onLocationFound(e) {
var radius = e.accuracy / 2;

L.marker(e.latlng).addTo(map)
.bindPopup("Position: " + e.latlng + " Genauigkeit " + radius ).openPopup();

L.circle(e.latlng, radius).addTo(map);
}

function onLocationError(e) {
alert(e.message);
}


function onMapClick(e) {
marker = new L.marker(e.latlng, {id:uni, icon:redIcon, draggable:'true'});
marker.on('dragend', function(event){
    var marker = event.target;
    var position = marker.getLatLng();
    alert(position);
    marker.setLatLng([position],{id:uni,draggable:'true'}).bindPopup(position).update();
});
map.addLayer(marker);
}     

//var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    //x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
//x.innerHTML = "Latitude: " + position.coords.latitude + 
//"<br>Longitude: " + position.coords.longitude;    
}

http://imgur.com/exOUZuT

最佳答案

我猜想初始化时 map 的大小是罪魁祸首。

Leaflet在初始化的时候需要知道它所嵌入的元素的大小。 Leaflet 使用该信息来了解要加载多少图 block 等。此外, map 大小的任何编程更改(或 Leaflet 无法轻易检测到的更改)都必须遵循 map.invalidateSize(..) link .

我怀疑在设置大小后,Leaflet 无法正确读取#map 元素的新大小。之后尝试使大小无效或异步运行初始化。我要补充:

setTimeout(function () {
    map.invalidateSize();
}, 0);

并检查它是否变得更好。

关于javascript - 传单仅加载一个图 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454717/

相关文章:

javascript - 传单 : ordering GeoJSON elements inside a layer

javascript - 传单路由不显示路径

javascript - 附近地点 Google map Places API

javascript - 英特尔XDK : editor doesn't recognize &lt;script&gt;

ios - 解析推送通知 : iOS device token is undefined

javascript - 如何在纯(普通)JavaScript 中修复我的可拖动代码,移动 div 的问题(div 跳跃或不移动)

javascript - 如何将 JSON 字符串化的 javaScript 对象发送到服务器

javascript - jQuery 访问父对象属性?

r - 计算从多边形的所有部分到最近点的距离

javascript - 需要反向滚动行为