Can I include a leaflet layer that can only be turned on with a password?

Viewed 193

I'm developing a treasure hunt with a leaflet map (from within R).
Participants can solve riddles along the way to receive the coordinates of a pre-hidden food package midway ;).

Is there a way to password-project the display of a leaflet layer?
It doesn't have to be secure, it's just a game, but it would be a nice gimmick.

What I have so far is a conditional display of the location like below.

leaflet() %>% addTiles() %>% setView(12.94, 52.39, 14) %>% 
  addCircleMarkers(12.94, 52.395, col="red", group="Riddle") %>% 
  addLayersControl(overlayGroups="Riddle", options=layersControlOptions(collapsed=FALSE)) %>% 
  hideGroup("Riddle")
1 Answers

If it's just a game and you don't want to write a whole shiny app, you can just add some javascript to have a similar effect. Obviously not secure though.

A working example can be found here: https://rpubs.com/Jumble/secret_layer

The password is: "Pikachu"

library(dplyr)
library(leaflet)
library(htmlwidgets)


leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addMarkers(lat=50.9, lng=-0.9, group="Not Secret") %>%
  setView(lat=37.8, lng=0.96, zoom=3) %>%
  addLayersControl(baseGroups = c("Not Secret", "Secret Location"), options = layersControlOptions(collapsed = F)) %>%
    htmlwidgets::onRender("function(el, x){
    var myMap = this;
    var secretmarker = L.marker([40, 20])
    myMap.on('baselayerchange', function(e){
       if (e.name === 'Secret Location'){
          var secretkey = prompt('Secret Key: '); 
          if (secretkey === 'Pikachu'){
            secretmarker.addTo(myMap);
          } else {
            alert('Incorrect Key');
          }
        } else {
          secretmarker.remove();
        }
    })
  }")
Related