【问题标题】:Leaflet map and marker not appears传单地图和标记未出现
【发布时间】:2015-10-21 20:53:28
【问题描述】:

我想创建一个应用程序,帮助用户搜索城市中的美食场所,以便他们将来可以再次访问。这个应用程序是我第一个使用传单的有趣项目。我想做的是制作简单的地图并从数据库中加载标记,其中标记仅在用户从类别中查询地点或在搜索栏中搜索时出现。我确实在 JSFiddle 中测试了 index page 和 marker.js,但它只出现在搜索按钮、栏和类别旁边的 map-wrap。

Marker.js

//now map reference the global map declared in the first line
map=L.map('map').setView(true)

//marker development

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        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>',
        maxZoom:18
     }).addTo(map);
    newMarkerGroup = new L.LayerGroup();
    map.on('click',addMarker);
 });

 //add group layer to map
 map.addLayer(search_group);

 //start to initiate adding marker
 function addMarker(e){
        // Add marker to map at click location; add popup window
        var newMarker = new L.marker(e.latlng).addTo(map);
    }

//create array
var marker= new Array();
function onMapClick(e){
    marker = new L.Marker(e.latlng,
{draggable: false});

//pushing items into array each by each and then add markers
function itemWrap(){
    for(i=0; i<items.length; i++){
        var LamMarker = new
    L.marker([items[i].lat,items[i].lon]);
        marker.push(LamMarker);
        map.addLayer(marker[i]);
    }
  }

 //create popup
  marker.bindPopup().openPopup();
  };

//add to map function with a popup that contains a link, which when clicked will have a popup of details and booking options
//this link will have as its id the latlng of the point
//this id will then be compared to when you click on one of your created markers 
map.on('click',function(e){
    var clickPositionMarker = L.marker([e.latlng.lat,e.latlng.lng],{icon:idMarker});
    clickArr.push(clickPositionMarker);
    mapLat = e.latlng.lat;
    mapLon = e.latlng.lng;
    clickPositionMarker.addTo(search_group).bindPopup().openPopup();

        L.marker(e.latlng).addTo(map)
            .bindPopup("You're within " + radius + " meters from this point").openPopup();

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

}

map.on('locationfound' , onLocationFound);

        //access it using markers[i]["id"]

我之前确实测试过 index.html,当然我取消了 L.marker 的注释 功能,它工作正常。确实出现了弹出窗口。但是当我测试标记时,将评论放在 L.marker 上,只出现 map-wrap 和其他 css。请帮助我,我确实查看了文档,测试了很多但没有出现。有人请帮助我,因为我对传单真的很陌生

【问题讨论】:

    标签: search leaflet marker


    【解决方案1】:

    您在L.marker 之前抛出了new,这不是此构造函数的格式。注意m 的大小写差异。它必须是L.marker(options)new L.Marker(options)。您只需要 newL.Marker 样式标记初始化。 见http://leafletjs.com/reference.html#marker

    【讨论】:

    • 非常感谢您的帮助。但是,为什么如果我对地图进行修改,比如修改标记或调整弹出窗口,地图就会变成空白?我必须更改瓷砖层吗?我是否必须在我当前构建的应用程序的每个部分中初始化切片层,因为我的应用程序包含弹出窗口、上传、标记和 map.js,或者只是在 index.html 中初始化它?我很困惑
    • 不知道为什么会这样……你能发个 jsfiddle 吗?
    • 它仍然是空白的,地图根本没有出现。你能告诉我编写 index.html 将标记链接到外部标记.js 的正确方法吗?我真的希望创建一个传单地图,从数据库中获取标记,并且仅在用户请求时出现。 mysql获取的信息指示标记通过geophp转换为geojson并在用户请求时将其指向地图
    • L.tileLayer('htt[://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '$copy; osm.org/copyright">OpenStreetMap</a> 贡献者的 }).
    • 抱歉打错了,上面是我目前使用的瓦片层
    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 2022-01-15
    • 2016-08-23
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多