Javascript 如何通过单击位于所述标记信息窗口中的按钮来更改标记的颜色(使用传单)?
我的地图上显示的标记来自我的数据库。每个标记都有一个带有两个按钮的信息窗口,“Recusar”和“Aceitar”,如果单击按钮“Aceitar”,我只想更改我先前选择的标记的颜色。如果点击“recusar”按钮,我希望删除地图上的标记。名为“aceite()”的函数不起作用Javascript 如何通过单击位于所述标记信息窗口中的按钮来更改标记的颜色(使用传单)?,javascript,asp.net-core,leaflet,asp.net-core-mvc,Javascript,Asp.net Core,Leaflet,Asp.net Core Mvc,我的地图上显示的标记来自我的数据库。每个标记都有一个带有两个按钮的信息窗口,“Recusar”和“Aceitar”,如果单击按钮“Aceitar”,我只想更改我先前选择的标记的颜色。如果点击“recusar”按钮,我希望删除地图上的标记。名为“aceite()”的函数不起作用 <div id="map"></div> <script> var map = L.map('map').setView([39.694502, -8.130573], 7
<div id="map"></div>
<script>
var map = L.map('map').setView([39.694502, -8.130573], 7);
L.tileLayer('https://api.maptiler.com/maps/topo/256/{z}/{x}/{y}.png?key=7YZ2VQZiOPItbtjSPYWe', {
attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>',
}).addTo(map);
$(document).ready(function () {
$.ajax({
type: 'GET',
url: '/api/IgnicoesAPI',
datatype: JSON,
success: function (data) {
$.each(data, function (i, item) {
var infowindow = '<div id="content">' +
'<div id="siteNotice">' +
'</div>' +
'<div id="bodyContent">' +
'<p><b>Avaliação da Ocorrência:</b></p>' +
'Fotografias:' + '<div class="slider-holder">' +
'<span id="slider-image-1"></span>' +
'<span id="slider-image-2"></span>' +
'<span id="slider-image-3"></span>' +
'<div class="image-holder" >' +
'<img src="https://notalentforcertainty.files.wordpress.com/2015/09/woods-on-fire.jpg?w=1200" class="slider-image" style="width:60px; height:60px" />' +
'<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTGlNA19yPsHNHW-n4mF3uhK7SnGkm029xfdQ1EhSyDPEmaGDM_" class="slider-image" style="width:60px; height:60px"/>' +
'<img src="https://wpde.com/resources/media/83f69602-210f-4ad1-bad0-bd5cf19470c7-large16x9_MARIONWOODSFIRE2.jpeg?1553519933510https://wpde.com/resources/media/83f69602-210f-4ad1-bad0-bd5cf19470c7-large16x9_MARIONWOODSFIRE2.jpeg?1553519933510" class="slider-image" style="width:60px; height:60px"/>' +
'</div>' +
'<div class="button-holder">' +
'<a href="#slider-image-1" class="slider-change"></a>' +
'<a href="#slider-image-2" class="slider-change"></a>' +
'<a href="#slider-image-3" class="slider-change"></a>' +
'</div>' +
'</div> ' +
'<p>Avaliação:' + item.estado + '</p>' +
'<p>Data:' + /*item.listaOcorrencias*/ + '</p></br>' +
'<button id="aceite" onclick="aceite()" >Aceitar</button>' +
'<button id="recusado">Recusar</button>' +
'<button id="concluido"> Concluído</button>' +
'</div>';
var greenIcon = L.icon({
iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
iconSize: [35, 35], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
var marker = new L.marker([item.latitude, item.longitude], {icon: greenIcon})
.bindPopup(infowindow)
.on('click', onClick)
.addTo(map);
$('#json map').append(marker);
});
},
error: function (err) {
console.error(err);
}
});
});
function onClick(e) {
var popup = e.target.getPopup();
var content = popup.getContent();
console.log(content);
}
function aceite(e) {
var greenIcon = L.icon({
iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
iconSize: [35, 35], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
marker = new L.marker({ icon: greenIcon })
.addTo(map);
}
var map=L.map('map').setView([39.694502,-8.130573],7);
L.tileLayer('https://api.maptiler.com/maps/topo/256/{z} /{x}/{y}.png?key=7yz2vqziopitbjspywe'{
归属:'',
}).addTo(地图);
$(文档).ready(函数(){
$.ajax({
键入:“GET”,
url:“/api/ignicosapi”,
数据类型:JSON,
成功:功能(数据){
$。每个(数据、功能(i、项){
var infowindow=''+
'' +
'' +
'' +
“阿瓦利亚昂·达奥科伦西亚:”+
“Fotografias:”+“+
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
' ' +
“Avaliação:”+item.estado+“”+
“数据:”+/*item.listaOcorrencias*/+”'+
“Aceitar”+
“Recusar”+
“结论”+
'';
var greenIcon=L.icon({
伊克努尔:'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
图标大小:[35,35],//图标的大小
阴影大小:[50,64],//阴影的大小
iconAnchor:[22,94],//对应于标记位置的图标点
shadowAnchor:[4,62],//对阴影也一样
popupAnchor:[-3,-76]//相对于iconAnchor打开弹出窗口的点
});
var marker=新的L.marker([item.latitude,item.longitude],{icon:greenIcon})
.bindPopup(信息窗口)
.on('click',onClick)
.addTo(地图);
$('#json map').append(marker);
});
},
错误:函数(err){
控制台错误(err);
}
});
});
函数onClick(e){
var popup=e.target.getPopup();
var content=popup.getContent();
控制台日志(内容);
}
函数aceite(e){
var greenIcon=L.icon({
伊克努尔:'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
图标大小:[35,35],//图标的大小
阴影大小:[50,64],//阴影的大小
iconAnchor:[22,94],//对应于标记位置的图标点
shadowAnchor:[4,62],//对阴影也一样
popupAnchor:[-3,-76]//相对于iconAnchor打开弹出窗口的点
});
marker=新的L.marker({icon:greenIcon})
.addTo(地图);
}
有人能帮我解决这个问题吗?将代码更改为:
var clickedmarker = null;
function onClick(e) {
var popup = e.target.getPopup();
var content = popup.getContent();
clickedmarker = e.target;
console.log(content);
}
function aceite() {
var greenIcon = L.icon({
iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
iconSize: [35, 35], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
if(clickedmarker){ //if the object exists and is not null / undefined
clickedmarker.setIcon(greenIcon);
}
clickedmarker = null;
}
//For deleting the marker:
function recusado(){
if(clickedmarker){ //if the object exists and is not null / undefined
clickedmarker.removeFrom(map);
}
clickedmarker = null;
}
L.marker
缺少aceite函数上的坐标,因此在aceite函数(按钮)上,我是否应该单击=“aceite(item.longitude,item.latitude)”,然后单击函数aceite(latitude,longitude)?就是这样!非常感谢你