传单js:如何为L.CircleMarker创建工具提示



我想知道是否有办法为 L.CircleMarker 设置工具提示?

var geojsonLayerVessel = new L.GeoJSON(null, {
    pointToLayer: function (latlng){
    return new L.CircleMarker(latlng, {
        radius: 5,
        fillColor: "#ff7800",
        color: "#000",
        weight: 1,
        opacity: 1,
        fillOpacity: 0.8,
        title: "test"
    });
}
}); 

尝试了上面的代码,但它不起作用。

对于 GeoJSON 图层,您可以侦听"featureparse"事件来绑定弹出窗口,如以下示例所示。大致如下:

var geoJsonLayer = new L.GeoJSON(null,{
pointToLayer: function (latlng){
return new L.CircleMarker(latlng, {
    radius: 5,
    fillColor: "#ff7800",
    color: "#000",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8,
});
geoJsonLayer.on('featureparse', function(e){
//Now you can bind popups to features in the layer, and you have access to
//attributes on the GeoJSON object through e.properties:
e.layer.bindPopup('Hello! ' + e.properties.someProperty);
});
//now you add some some data to your layer and add it to the map....
geoJsonLayer.addGeoJSON(someGeoJson);
map.addLayer(geoJsonLayer);

希望这有帮助!

不适用于CircleMarkers。但是您可以创建一个小DivIcon并用圆角设置样式。 DivIcon确实支持'title'选项。

http://jsfiddle.net/63teycsq/1/

作为点到层提供的函数:

function (latlng){
    return L.marker(latlng, 
                    { icon : L.divIcon({ className : 'circle',
                                         iconSize : [ 5, 5 ]}),
                      title: 'test'});
}

以及div 的样式:

div.circle {
    background-color: #ff7800;
    border-color: black;
    border-radius: 3px;
    border-style: solid;
    border-width: 1px;
    width:5px;
    height:5px;
}

事实证明,在最新版本的Leaflet中,CircleMarker确实有工具提示,因此使用原始代码可以实例化为:

var geojsonLayerVessel = new L.GeoJSON(null, {
    pointToLayer: function (latlng){
    return new L.CircleMarker(latlng, {
        radius: 5,
        fillColor: "#ff7800",
        color: "#000",
        weight: 1,
        opacity: 1,
        fillOpacity: 0.8
    }).bindTooltip("test");
}
}); 

最新更新