【发布时间】:2015-02-13 04:31:21
【问题描述】:
大家好,我只是想问一些关于传单标记的事情。
我有一个数据库和一个表,其中有一个名为 icon_name 的字段,它看起来像这样。
|icon_name|
___________
|FIRE |
|HOMICIDE |
|RAINSTORM|
我还有一个名为 Legends 的文件夹,它有一个类似这样的文件。
Folder Name: Legends
Files Inside:
FIRE.jpg
HOMICIDE.jpg
RAINSTORM.jpg
如您所见,我的表中 jpg 文件的名称是相同的 (icon_name)
我下面的代码是调用文件夹中的特定图像,并将其用作传单标记图像,然后将其放入地图中(注意:以下代码将标记信息从数据库生成到地图中)
var Icon1 = L.icon({ //<---Assigning a name of an image into a Leaflet Icon.
iconUrl: 'legends/FIRE.jpg',//<--- Image Folder Location + the Image Name
iconSize: [50, 50], //<--- Size of the image converted as icon
iconAnchor: [23, 50], //<--- Icons Anchor
popupAnchor: [3, -50] //<--- Leaflet Pop up
function getInfo() {
$.getJSON("get_info.php", function (data) {
for (var i = 0; i < data.length; i++) {
var location = new L.LatLng(data[i].lat, data[i].lng);
var marker = new L.Marker(location,{icon:Icon1});
marker.bindPopup(
data[i].name + "<br>" +
data[i].user_date + "<br>" +
data[i].user_time + "<br>" +
data[i].address + "<br>"
).addTo(map);
marker.on('click', function(e) { // HERE YOU GO
var ll = marker.getLatLng();
document.querySelector('#userLat1').value = ll.lat;
document.querySelector('#userLng1').value = ll.lng;
alert('You have selected a Marker that will be deleted');
});
}
});
}
现在上面代码的输出是这样的:
来自地图不同部分的许多标记(好) 所有这些标记图像都只是“FIRE.jpg”,即使其他标记的 "icon_name" 是 HOMICIDE、RAINSTORM 等。
现在这是我的大问题:如何将表字段 icon_name 的值传递给 var Icon1 或 iconUrl
的值var Icon1 = L.icon({
iconUrl: 'legends/FIRE.jpg',
这样输出就会有不同的标记和不同的标记图像。我希望你能理解我。 TY
根据我给出的答案,这是我测试过的代码,但它不起作用。
(部分代码)
for (var i = 0; i < data.length; i++) {
var location = new L.LatLng(data[i].lat, data[i].lng);
var Icon1 = L.icon({
iconUrl: 'legends/FIRE.jpg',
marker.bindPopup(
data[i].name + "<br>" +
data[i].user_date + "<br>" +
data[i].user_time + "<br>" +
data[i].address + "<br>"
).addTo(map);
【问题讨论】:
-
你的 get_info.php 文件是什么样的?
-
先生,我的 get_info.php 只是一个 php 文件,其中包含我表中的选择命令代码,因为我需要在将其转换为传单函数之前调用选择命令。
标签: javascript php html ajax