如何在L.geoJSON pointToLayer中实例化Marker

时间:2018-05-18 23:15:05

标签: javascript leaflet

当我包含以下代码时,不会出现任何标记,并且控制台中的错误为:

  

未捕获的TypeError:this.callInitHooks不是pointToLayer的函数。

如果您知道任何解决方案,请分享。

var map = L.map('map', {
            center: [53.423933, -7.94069], 
            zoom: 7,
            layers: [grayscale]
        });

var url = "howdy.json";

var geojsonMarkerOptions = L.icon({
            iconUrl: 'howdy.png',
            iconSize:     [16, 28], 
            iconAnchor:   [8, 18], 
            popupAnchor:  [-3, -13] 
        });

function forEachFeature(feature, layer) {

        var popupContent = 
        feature.properties.Cabin+
        feature.properties.Crew + 
        feature.properties.Mobile;

        if (feature.properties && feature.properties.popupContent) {
            popupContent += feature.properties.popupContent;
        }
            layer.bindPopup(popupContent);
        };

      var howdy = L.geoJSON(null, {
        onEachFeature: forEachFeature, 
        pointToLayer: function (feature, latlng) {
            return L.Marker(latlng, geojsonMarkerOptions);
        }
  });

$.getJSON(url, function(data) {
        Shelter.addData(data);
});

Shelter.addTo(map);

1 个答案:

答案 0 :(得分:1)

很可能错误是由您的行引起的:

return L.Marker(latlng, geojsonMarkerOptions);

...您尝试在没有触发实例化的L.Marker JavaScript关键字的情况下实例化调用new 类构造函数的Leaflet标记。

请注意与Leaflet提供的 factory L.marker(小写首字母m)的区别,这只是new L.Marker的别名。