我在尝试保存用户放置的 map 上的传单标记时遇到问题。目前,他们单击并在他们单击的位置添加了标记。再次单击后,它将放大,并打开一个弹出窗口,其中有两个字段用于输入信息,最后有一个提交按钮。他们还可以拖动或单击来移动标记,而无需创建更多标记。
理想情况下,这将链接到数据库以存储用户的输入。做这件事的人正在使用 SQlite3 和 Ruby/Rails,我对它们知之甚少。现在,当我在搜索栏中使用 GET 填写数据后单击“提交”时,页面会刷新并且标记会消失。是否可以让标记也出现在 map 上?我尝试四处搜索,但没有出现任何以这种方式添加标记的内容
这是我到目前为止所拥有的代码,无论如何我都不是专家。这可能解释了为什么我无法弄清楚这一点。
// User Marker
var currentMarker;
map.on("click", function (event) {
if (currentMarker) {
currentMarker._icon.style.transition = "transform 0.3s ease-out";
currentMarker._shadow.style.transition = "transform 0.3s ease-out";
currentMarker.setLatLng(event.latlng);
setTimeout(function () {
currentMarker._icon.style.transition = null;
currentMarker._shadow.style.transition = null;
}, 300);
return;
}
currentMarker = L.marker(event.latlng, {
draggable: true,
icon: L.AwesomeMarkers.icon({
icon: 'exclamation-circle',
markerColor: 'orange',
iconColor: '#F8FAEE',
})
}).addTo(map).on("click", function (e) {
map.setView(e.latlng, 17);
alert("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)
event.originalEvent.stopPropagation();
}).bindPopup('<form role="form" id="form" onsubmit="addMarker()">'+
'<div class="form-group">'+
'<label class="control-label col-sm-10"><strong>Type of Complaint </strong></label>'+ "<br>" +
'<select class="form-control" id="toc" name="toc">'+
'<option value="Pothole">Pothole</option>'+
'<option value="Construction">Construction</option>'+
'<option value="Road Closed">Road Closed</option>'+
'<option value="Other">Other...</option>'+
'</select>'+
'</div>'+
'<div class="form-group2">'+
'<label class="control-label col-sm-10"><strong>Description of Complaint </strong></label>'+ "<br>" +
'<input type="text" placeholder="Extra Information" id="doc" name="extra" class="form-control"/>'+
'</div>'+
'<div class="form-group">'+
'<div style="text-align:center;" class="col-xs-11"><button style="text-align:center;" id="submit" value="submit" class="btn btn-primary trigger-submit">Submit</button></div>'+
'</div>'+ "<br>" +
'</form>');
});
document.getElementById("done").addEventListener("click", function () {
currentMarker = null;
});
如果需要更多信息,请告诉我,希望有人能给我指出正确的方向。谢谢。
最佳答案
页面已刷新,因为您的表单触发了提交事件。当触发提交时,页面将向同一页面本身发送 GET 请求,因为未定义目标。要禁用刷新,您必须向表单的 onsubmit 属性返回 false 值。
绑定(bind)弹出窗口时,请使用该方法添加 return 语句。
'<form role="form" id="form" onsubmit="return addMarker();">'+
其次定义addMarker方法。在此方法中,您可以向服务添加 AJAX 请求以发送数据并将其异步推送到数据库。
var addMarker = function(){
//Get the data from the form & send it to the service.
$.ajax({
method: "POST",
url: "someservice.php",
data: { toc: "your type", description: " your description" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
return false; //Don't submit anything
}
关于javascript - 可以通过提交按钮将用户提交的标记添加到传单 map 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46554481/