简体   繁体   English

使用tabletop.js的传单地图中的标记为何不起作用?

[英]Markers in leaflet map using tabletop.js Why its not working?

I'm noob to javascript... I tried to find a solution to a problem on a similar thread , but I don't know why it doesn't work in my case. 我对javascript没什么看法...我试图在类似的线程上找到问题的解决方案,但我不知道为什么在我的情况下它不起作用。 I can't comment there so I add new question. 我无法在此处发表评论,所以我添加了新问题。

All my code is here: Link to Fiddle 我所有的代码在这里: 链接到小提琴

When I changed code with example, the map stopped showing. 当我用示例更改代码时,地图停止显示。 Where have I done the mistake? 我在哪里做错了? Thank You for helping me. 感谢你们对我的帮助。

Main part of js is like this: js的主要部分是这样的:

function myFunction() {

// Add MAP
var map = L.map('map').setView([54.151, 17.823], 9);
var basemap = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
    minZoom: 5,
    maxZoom: 19
});
basemap.addTo(map);

/* ----------------------------------------------- */
// ZOOMING TO DETECT LOCATION
// map.locate({setView: true, maxZoom: 16});
/* ----------------------------------------------- */
// INFO ABOUT DETECT LOCATION      
function onLocationFound(e) {
    var radius = e.accuracy;

    L.marker(e.latlng).addTo(map)
        .bindPopup("Jesteśw odległości " + radius + " [m] od tego punktu.").openPopup();

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

map.on('locationfound', onLocationFound);

// ERROR DETECT LOCATION
function onLocationError(e) {
    alert(e.message);
}

// MOUSE CLICK POSITION
var popup = L.popup();

function onMapClick(e) {
    popup
        .setLatLng(e.latlng)
        .setContent("" + e.latlng.toString())
        .openOn(map);
}

map.on('click', onMapClick);

/* ----------------------------------------------- */
// MARKERS
//  var pkt = L.marker([54.46791, 17.0288]).addTo(map).bindPopup("CIO");
/* ----------------------------------------------- */

function addPoints(data, tabletop) {
    for (var row in data) {
        var marker = L.marker([
            data[row].Latitude,
            data[row].Longitude
        ]).addTo(map);
        marker.bindPopup('<strong>' + data[row].Info + '</strong>');
    }
}

function init() {
    Tabletop.init({
        key: 'https://docs.google.com/spreadsheets/d/1BTlWo-T636OCGK-tRMHRP55MQ24OwQ-ZF9yOszyppxk/edit?usp=sharing',
        callback: addPoints,
        simpleSheet: true
    })
}
init()
}

The code in JSFiddle doesn't work because the default JSFiddle javascript Load Type setting is On Load . JSFiddle中的代码不起作用,因为默认的JSFiddle javascript加载类型设置为On Load This means that it will load everything that you have in JavaScript tab without you needing to load anything manually. 这意味着它将加载JavaScript选项卡中的所有内容,而无需手动加载任何内容。

But your whole thing initializes with onload callback to myFunction() and it will never fire with that setting enabled. 但是,您的整个过程都会使用myFunction() onload回调进行初始化,并且在启用该设置的情况下永远不会触发。 You have two possible solutions: 您有两种可能的解决方案:

  • if you don't want to change anything in Fiddle configuration, than you need to remove onload from the body tag and change myFunction() into self-invoking function: 如果您不想在Fiddle配置中进行任何更改,则需要从body标签中删除onload并将myFunction()更改为自调用函数:

    (function () { **body of the function** }())

  • or just edit the Fiddle settings (change Load type to No wrap - Bottom of ): 或仅编辑Fiddle设置(将“加载类型”更改为“无环绕-底部”): 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM