Search code examples
rleafletmappingmaps

Add a title to the Leaflet map


I have a simple question but still do not know how to find the answer to it. All I need is to add a title to the leaflet map. I do not need it to me on the map, just above it as in a usual ggplot map.

My code is:

map <- leaflet(data_counts) %>% 
  addTiles() %>%
  addCircleMarkers(lng=data_counts$Long, lat=data_counts$Lat, 
             color="#2c4f54", fillOpacity=0.6,
             stroke = FALSE,
             radius=~sqrt(NumObs)/pi*10,
             label=~paste(data_counts$`Birth place...14`), labelOptions = labelOptions(noHide = F)) 

I have tried different options but still no success


Solution

  • Not sure if you already found a solution but you could try this:

    library(leaflet)
    library(tidyverse)
    library(htmltools)
    
    
    tag.map.title <- tags$style(HTML("
    .leaflet-control.map-title {
    transform: translate(50%,50%);
    position: fixed !important;
    left: 45%;
    text-align: center;
    padding-left: 10px;
    padding-right: 10px;
    background: rgba(255,255,255,0.75);
    font-weight: bold;
    font-size: 28px;
    }
    "))
    
    title <- tags$div(
      tag.map.title, HTML("This is the Title")
    )
    
    map <- leaflet(data_counts) %>% 
      addTiles() %>%
      addCircleMarkers(lng=data_counts$Long, lat=data_counts$Lat, 
                 color="#2c4f54", fillOpacity=0.6,
                 stroke = FALSE,
                 radius=~sqrt(NumObs)/pi*10,
                 label=~paste(data_counts$`Birth place...14`), labelOptions = 
    labelOptions(noHide = F))  %>%
    addControl(title, position = "topleft", className="map-title")