我使用leaflet
包创建了一个地图窗口小部件,然后使用函数addMarkers()
添加了两个图层。但是,我发现地图上的点在放大和缩小时会改变它们的位置。相关脚本如下:
首先,在pchIcons
leaflet
pchIcons <- function(pch = 0:14, width = 30, height = 30, col = 1:15, ...) {
pchLength <- length(pch)
pchFiles <- character(pchLength)
# create a sequence of png images
for (i in seq_len(pchLength)) {
pchTempFile <- tempfile(fileext = '.png')
png(pchTempFile, width = width, height = height, bg = 'transparent')
par(mar = c(0, 0, 0, 0))
plot.new()
points(.5, .5, pch = pch[i], cex = 1.5, col = col[i], ...)
dev.off()
pchFiles[i] = pchTempFile
}
pchFiles
}
然后创建一个具有巴黎位置的基础图层,并添加两个图层,每个图层代表一个组。
baseLayer <- leaflet(zero) %>%
addProviderTiles("Stamen.Toner") %>%
setView(posParis[["lng"]], posParis[["lat"]], zoom = 12)
addZeroLayer <- baseLayer %>%
addMarkers(popup = paste(paste(zero$Code, zero$Ecart),
zero$Address,
sep = "<br/>"),
lng = as.numeric(zero$Long),
lat = as.numeric(zero$Lat),
icon = ~ icons(
iconUrl = pchIcons(rep(magasinEcart[1], nrow(zero)),
40,
40,
col = colorZero,
lwd = 2)
),
group = '0')
addOneLayer <- addZeroLayer %>%
addMarkers(popup = paste(paste(one$Code, one$Ecart),
one$Address,
sep = "<br/>"),
lng = as.numeric(one$Long),
lat = as.numeric(one$Lat),
icon = ~ icons(
iconUrl = pchIcons(rep(magasinEcart[2], nrow(one)),
40,
40,
col = colorOne,
lwd = 2)
),
group = '1')
接下来,使用addLayersControl()
添加UI控件以打开和关闭图层,并使用addLegend()
添加图例。
mapTourist <- addOneLayer %>%
addLayersControl(overlayGroups = c('0', '1'),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend("bottomright",
pal = colorFactor(brewer.pal(9, 'Set1')[3:4],
unique(magasinBaseComp$Ecart)),
values = names(magasinEcart),
title = "Ecart",
opacity = 1)
最后,我们可以发现,在放大或缩小时,地图上的点会移动(如屏幕截图所示)。
有人知道问题是什么,我该如何解决?欢迎任何想法!