0

我使用 cloudmade 和 Leaflet 插件创建了一个地图。我设置了我的标记,我的目标是创建我自己的带有 3 个位置链接的控制面板。当您单击一个链接(例如伦敦)时,它将平移到存储的伦敦经纬度,其他两个相同。我加载地图的代码但容器消失了,我在控制台中收到以下错误消息:“Mycontrol.appendhild(link);- Mycontrol 未定义。定义时这仍然不起作用。

1)我在正确的路线上吗?2)任何帮助在示例或教程上工作都会很棒

谢谢。

这是代码片段

[代码]

[Cloudmade API Key and URL]

[Leaflet ICON Properties]
[Leaflet MARKERS]

[Leaflet toggle styles]

//CONTROL

var MyControl = L.Control.extend({
    options: {
        position: 'topright'
    },

    onAdd: function (map, position) {
        // create the control container
        var container = L.DomUtil.create('div', 'my-custom-control');

        // ... initialize other DOM elements, add listeners, etc.
        function createMapLink(location, zoom, name) {
            var link = document.createElement('a');
            link.href = '';
            link.innerHTML = "Go to " + name;
            link.onclick = function() {
                map.setCenter(location, zoom);
                return false;
            }
            Mycontrol.appendChild(link);
        }

        createMapLink(new L.LatLng(lat,lon), 11, 'Location1');
        createMapLink(new L.LatLng(lat,lon), 11, 'Location2');
        createMapLink(new L.LatLng(lat,lon), 11, 'Location3');

        return container;
    }
});

map.addControl(new MyControl());


L.control.layers(baseMaps, overlayMaps, MyControl).addTo(map);

[/代码]

4

1 回答 1

0

您正在尝试扩展 Control 类,但将结果分配给 var。一个新类不是 var,而是一个函数,所以一开始就去掉 var 关键字。

查看传单缩放控件的来源,了解如何进行操作。

于 2013-01-13T21:18:34.413 回答