R小册子默认显示多边形标签

时间:2017-10-17 11:03:51

标签: html r leaflet

我是传单包的新手。
我试图绘制两种类型的多边形,让用户选择它们并查看边框。这些多边形有标签,我想默认显示它们。此时标签仅在鼠标悬停时显示。
基本上我想要的是让用户在地图上搜索多边形标签。
以下是我的代码。

shp <- readOGR(dsn = 'shapes'
               ,layer = 'SAB')

postcode <- readOGR(dsn = 'shapes'
               ,layer = 'Postcode')

CRS_WGS84 <- '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'

t_shp <- spTransform(shp, CRS(CRS_WGS84))

sab_shp <- raster::aggregate(t_shp, by='SMALL_AREA')


dat <- data.table(shp@data)

sabLabels <- sprintf('<strong>SAB: %s', t_shp$SMALL_AREA) %>% lapply(HTML)

postcode <- readOGR(dsn = 'shapes'
                    ,layer = 'Postcode')

CRS_WGS84 <- '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'

t_shp2 <- spTransform(postcode, CRS(CRS_WGS84))

postcode_shp <- raster::aggregate(t_shp2, by='RoutingKey')


dat2 <- data.table(postcode@data)

postcodeLabels <- sprintf('<strong>SAB: %s', t_shp2$RoutingKey) %>% lapply(HTML)


leaflet() %>%
  addTiles() %>%  #using default does not allow html export to include the underlying
  #OSM layer
  addProviderTiles('OpenStreetMap.Mapnik') %>%
  addPolygons( data = t_shp
               ,stroke = T
               ,fillColor = 'grey'
               ,fillOpacity = 0.2
               ,color = 'blue'
               ,weight = 0.5
               ,label = sabLabels
               ,group = 'SABS'
               ,highlightOptions = highlightOptions(color = "blue", weight = 7,
                                                    bringToFront = TRUE)
               #,labelOptions = labelOptions(noHide = TRUE, textOnly = TRUE, opacity = 0.5 , textsize='15px')

  ) %>%

  addPolygons( data = t_shp2
               ,stroke = T
               ,fillOpacity = 0
               ,color = 'black'
               ,weight = 1.5
               ,label = postcodeLabels
               ,group = 'PostCodes'
  ) %>%
addLayersControl(
  overlayGroups = c(
    'SABS'
    ,'PostCodes'

  )
  ,options = layersControlOptions((collapsed = F))
)

0 个答案:

没有答案