【发布时间】:2015-04-23 08:11:45
【问题描述】:
我正在使用 LeafletR 包 (http://cran.r-project.org/web/packages/leafletR/index.html) 来制作一个传单网络地图小程序,但在将 2 组功能加载到同一个地图上时遇到了问题。
据我了解,leaflet() 函数只接受一种几何类型的 GeoJSON 文件。
因此,我有 2 个单独的 GeoJSON 文件,一个带有 MultiPolygons,另一个带有 Points。
我可以使用以下代码将 MultiPolygons 渲染为等值线:
#Load LeafletR
require(leafletR)
#Create quantiles
cuts <- round(quantile(UKpostcode_areas$data, probs = seq(0, 1, 0.20), na.rm = FALSE), 0)
cuts[1] <- 0 #We don't want any negative values, so let's make the first cut zero
#Fields to include in the popup
popup.1 <- c("name", "data")
#Graduated style based on an attribute
sty.1 <- styleGrad(prop = "data", breaks=cuts, right=FALSE, style.par="col", style.val=rev(heat.colors(6)), leg="Data", lwd=1)
#Create the map and load into browser
map <- leaflet(data = "map/UKpostcode_areas.geojson", dest = "map", style = sty.1, title = "UKpostcode_areas_choropleth", base.map= "osm", incl.data=TRUE, popup = popup.1)
我还可以获取要渲染的点:
#Create new style and popup details for the 2nd layer
sty.2 <- styleSingle(col = "white", fill = "#2b83ba", fill.alpha = 1, rad = 3)
popup.2 <- c("name", "trust")
#Let's take a look at the map of hospitals
map2 <- leaflet(data="map/hospitals.geojson", dest = "map", style = sty.2, popup = popup.2, title = "hospitals", base.map = "osm", incl.data=TRUE, controls = "all")
browseURL(map2)
但是,当我尝试在同一张 Leaflet 地图上渲染两者时,它只会给我一个空白屏幕:
#Now we can combine the 2 into 1 map, this is problematic, can't get it to work!
map3 <- leaflet(data = list("map/UKpostcode_areas.geojson", "map/hospitals.geojson"), style = list(sty.1, sty.2), dest = "map", title = "index", base.map= "osm", incl.data=TRUE, controls = "all")
browseURL(map)
我怀疑最后几行代码有问题。但我不知道是什么。
【问题讨论】: