有什么方法可以使用正确的颜色和图标为addAwesomeMarkers函数创建传单图例。例如,在下面的地图中,我想创建代码下给出的图例。
library(leaflet)
IconSet <- awesomeIconList(
ship = makeAwesomeIcon(icon= 'ship', markerColor = 'green', iconColor = 'white', library = "fa"),
pirate = makeAwesomeIcon(icon= 'fire', markerColor = 'blue', iconColor = 'white', library = "fa")
)
# Some fake data
df <- sp::SpatialPointsDataFrame(
cbind(
(runif(20) - .5) * 10 - 90.620130, # lng
(runif(20) - .5) * 3.8 + 25.638077 # lat
),
data.frame(type = factor(
ifelse(runif(20) > 0.75, "pirate", "ship"),
c("ship", "pirate")
))
)
leaflet(df) %>% addTiles() %>%
# Select from oceanIcons based on df$type
addAwesomeMarkers(icon = ~IconSet[type])
Run Code Online (Sandbox Code Playgroud)
在此答案中,有一种可以执行此操作的方法,即通过插入地图控件并使用html定义控件。与其他答案不同,图标使用css样式创建图像(一个元素创建标记形状,另一个元素包含图标a div
和a span
)。图像来自分配给每个元素的css类:
<i>
)设置图标和图标的颜色(尽管对于font-awesome而言,图标的颜色似乎没有变化)每个图标库使用不同的类和稍有不同的约定。
给定另一个答案中引用的方法以及图标的属性,我构建了一个显示图标图例的基本功能。
我确实设法构建了一个功能,该功能可以从三个受支持的传单图标库(离子,超棒字体,glyphicon)中的每个定位图标,但是每个定位元素的定位属性略有不同,这仍然给我带来了较小的定位问题。为了使示例代码更简短,我只包括了字体很棒的定位,其他人的定位也遵循类似的方法。如果需要,我可以发布该功能的版本并支持所有这三个版本。
该函数仅创建html,您需要将其放置在控件中(它是基本的,可以轻松添加一些参数来对其进行自定义):
# legend html generator:
markerLegendHTML <- function(IconSet) {
# container div:
legendHtml <- "<div style='padding: 10px; padding-bottom: 10px;'><h4 style='padding-top:0; padding-bottom:10px; margin: 0;'> Marker Legend </h4>"
n <- 1
# add each icon for font-awesome icons icons:
for (Icon in IconSet) {
if (Icon[["library"]] == "fa") {
legendHtml<- paste0(legendHtml, "<div style='width: auto; height: 45px'>",
"<div style='position: relative; display: inline-block; width: 36px; height: 45px' class='awesome-marker-icon-",Icon[["markerColor"]]," awesome-marker'>",
"<i style='margin-left: 8px; margin-top: 11px; 'class= 'fa fa-",Icon[["icon"]]," fa-inverse'></i>",
"</div>",
"<p style='position: relative; top: -20px; display: inline-block; ' >", names(IconSet)[n] ,"</p>",
"</div>")
}
n<- n + 1
}
paste0(legendHtml, "</div>")
}
Run Code Online (Sandbox Code Playgroud)
并且,还添加了控件(请注意,它从图标列表中获取了图例的名称,因此我从您的原始名称中进行了修改,但其他所有内容都应相同):
library(leaflet)
# legend html generator:
markerLegendHTML <- function(IconSet) {
# container div:
legendHtml <- "<div style='padding: 10px; padding-bottom: 10px;'><h4 style='padding-top:0; padding-bottom:10px; margin: 0;'> Marker Legend </h4>"
n <- 1
# add each icon for font-awesome icons icons:
for (Icon in IconSet) {
if (Icon[["library"]] == "fa") {
legendHtml<- paste0(legendHtml, "<div style='width: auto; height: 45px'>",
"<div style='position: relative; display: inline-block; width: 36px; height: 45px' class='awesome-marker-icon-",Icon[["markerColor"]]," awesome-marker'>",
"<i style='margin-left: 8px; margin-top: 11px; 'class= 'fa fa-",Icon[["icon"]]," fa-inverse'></i>",
"</div>",
"<p style='position: relative; top: -20px; display: inline-block; ' >", names(IconSet)[n] ,"</p>",
"</div>")
}
n<- n + 1
}
paste0(legendHtml, "</div>")
}
IconSet <- awesomeIconList(
"Regular Ship" = makeAwesomeIcon(icon= 'ship', markerColor = 'green', iconColor = 'white', library = "fa"),
"Pirate Ship" = makeAwesomeIcon(icon= 'fire', markerColor = 'blue', iconColor = 'white', library = "fa")
)
# Some fake data
df <- sp::SpatialPointsDataFrame(
cbind(
(runif(20) - .5) * 10 - 90.620130, # lng
(runif(20) - .5) * 3.8 + 25.638077 # lat
),
data.frame(type = factor(
ifelse(runif(20) > 0.75, "Pirate Ship", "Regular Ship"),
c("Regular Ship", "Pirate Ship")
))
)
leaflet(df) %>% addTiles() %>%
addAwesomeMarkers(icon = ~IconSet[type]) %>%
addControl(html = markerLegendHTML(IconSet = IconSet), position = "bottomleft")
Run Code Online (Sandbox Code Playgroud)