gpt4 book ai didi

javascript - 为什么传单的 resetStyle 对我不起作用?

转载 作者:行者123 更新时间:2023-11-30 16:18:46 25 4
gpt4 key购买 nike

我想画一张 map ,上面只画了几条路线。

我想要一个带有数字 1,..,n 的保管箱

当选择下拉框中的元素时,相应的路线会在 map 上突出显示。

enter image description here

我已经开始使用“传单”了。

enter image description here

为什么我的 resetStyle() 没有将线条恢复到原来的样式?

这是我的代码:

document.onload = loadMap();

function loadMap() {
var map = L.map('map').setView([37.8, -96], 4);


L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
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>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1IjoiZW==========yJ9.3HqHQ4BMRvSPaYe8ToA7YQ'
}).addTo(map);


var marker = L.marker([51.5, -0.09]).addTo(map);


var myLines = [{
"type": "LineString",
"properties": {
"id": "1"
}
"coordinates": [
[-100, 40],
[-105, 45],
[-110, 55]
]
}, {
"type": "LineString",
"properties": {
"id": "2"
}
"coordinates": [
[-105, 40],
[-110, 45],
[-115, 55]
]
}];

var myLayer = L.geoJson().addTo(map);
myLayer.addData(myLines);


geojson = L.geoJson(myLines, {
onEachFeature: onEachFeature
}).addTo(map);

}



function highlightFeature(e) {
var layer = e.target;

layer

layer.setStyle({
weight: 25,
color: '#ff3300',
dashArray: '',
fillOpacity: 0.7
});

if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
}

function resetHighlight(e) {
geojson.resetStyle(e.target);


layer.setStyle({
weight: 5,
color: '#0000ff',
dashArray: '',
fillOpacity: 0.7
});
}


function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
// click: zoomToFeature
});
}

$('select[name="dropdown"]').change(function() {

var item = $(this).val();
alert("call the do something function on option " + item);
//how to make the chosen line highlighted ??

});

最佳答案

L.GeoJSONresetStyle 方法将给定图层的样式重置为初始化L.GeoJSON 图层时定义的样式:

Resets the given vector layer's style to the original GeoJSON style, useful for resetting style after hover events.

http://leafletjs.com/reference.html#geojson-resetstyle

代码示例:

var geojsonLayer = new L.GeoJSON(geojson, {
style: function () {
return {
color: 'red'
}
},
onEachFeature: function (feature, layer) {
layer.on('mouseover', function () {
this.setStyle({
color: 'green'
});
});
layer.on('mouseout', function () {
geojsonLayer.resetStyle(this);
});
}
}).addTo(map);

Plunker 上的工作示例:http://plnkr.co/edit/iriGMBYiFRizeXizMF06?p=preview

关于javascript - 为什么传单的 resetStyle 对我不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com