1) 同步两张地图
安装开发版本为我解决了这个问题
# Dependencies
# If your devtools is not the latest version
# then you might have to install "units" manually
install.packages('units')
install.packages('devtools')
library(devtools)
devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
我使用的代码:
library(leaflet)
library(ggmap)
library(mapview)
library(raster)
library(magrittr)
UK <- ggmap::geocode("United Kingdom")
#FILE1 <- read.csv("DATASET1.csv")
#FILE2 <- read.csv("DATASET2.csv")
FILE1 <- data.frame('lat' = c(51.31, 51.52, 51.53), 'lon' = c(0.06, 0.11, 0.09))
FILE2 <- data.frame('lat' = c(52.20, 52.25, 52.21), 'lon' = c(0.12, 0.12, 0.12))
map1 <- leaflet(FILE1)%>%
addTiles()%>%
addMarkers(clusterOptions = markerClusterOptions())
map2 <- leaflet(FILE2)%>%
addTiles()%>%
addMarkers(clusterOptions = markerClusterOptions())
mapview::latticeView(map1, map2, ncol = 2, sync = list(c(1, 2)), sync.cursor = FALSE, no.initial.sync = FALSE)
# Or:
sync(map1, map2)
2) 叠加两张地图
您可以使用两个单独的数据框作为数据源,并将它们分别添加到同一个地图中。更改符号样式以区分它们。
map3 <- leaflet(FILE2)%>%
addTiles() %>%
addCircleMarkers(data = FILE1) %>%
addCircleMarkers(data = FILE2,
color = '#0FF')
map3
如果你想对集群标记做类似的事情,这里和这里有一些很好的文档。基于这些帖子中的一些代码,我在下面创建了一个建议,我使用预先存在的样式来区分不同类型的集群:
FILE1 <- data.frame('lat' = rnorm(n = 1000, mean = 51.4, sd = 0.5),
'lon' = rnorm(n = 1000, mean = 0.8, sd = 0.5))
FILE2 <- data.frame('lat' = rnorm(n = 1000, mean = 53, sd = 0.5),
'lon' = rnorm(n = 1000, mean = -0.5, sd = 0.5))
map3 <- leaflet(rbind(FILE1, FILE2)) %>%
addTiles() %>%
addCircleMarkers(data = FILE1,
color = '#FA5',
opacity = 1,
clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {
var childCount = cluster.getChildCount();
var c = ' marker-cluster-';
if (childCount < 3) {
c += 'large';
} else if (childCount < 5) {
c += 'large';
} else {
c += 'large';
}
return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>',
className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
}"))) %>%
addCircleMarkers(data = FILE2,
color = '#9D7',
opacity = 1,
clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {
var childCount = cluster.getChildCount();
var c = ' marker-cluster-';
if (childCount < 3) {
c += 'small';
} else if (childCount < 5) {
c += 'small';
} else {
c += 'small';
}
return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>',
className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
}")))