Search code examples
rleafletgeospatialr-leaflet

Create smallest grid in leaflet R


I want to create the smallest grid as possible in leaflet R. How should I go about it ?

My current code is :-

leaflet()%>%
  addTiles() %>%
  setView(lng = 101.9758, lat = 4.21053, zoom = 10)) %>%
  addGraticule(interval = 0.02, sphere = FALSE) %>%
  addMarkers(101.6995, 3.1473) 

Here the picture output from the code before zoom Image before zoom in

Here the picture after zoom in Image after zoom in

The grid quite large after I zoom in but if I change addGraticule(interval = 0.01), my laptop hang and no output result. I want the grid to be small as picture below

Desired result

Is there other way I can achieve my aim?

I found other function that might help which is

addSimpleGraticule(
  map,
  interval = 20,
  showOriginLabel = TRUE,
  redraw = "move",
  hidden = FALSE,
  zoomIntervals = list(),
  layerId = NULL,
  group = NULL
)

zoomIntervals :- use different intervals in different zoom levels. If not specified, all zoom levels use value in interval option.

But I'm not sure how to specify zoomIntervals arguments.


Solution

  • The Documentation of the R function addSimpleGraticule is not very verbose, but you can get a hint about the required data structure here.

    You can set the number of grid lines per meter depending on the zoom level indeed using the zoomIntervals option:

    library(leaflet)
    
    leaflet() %>%
      addTiles() %>%
      setView(lng = 101.6995, lat = 3.1473, zoom = 20) %>%
      addMarkers(101.6995, 3.1473) %>%
      addSimpleGraticule(
        showOriginLabel = TRUE,
        redraw = "move",
        hidden = FALSE,
        zoomIntervals = list(
          list(start = 1, end = 3, interval = 10),
          list(start = 4, end = 9, interval = 1),
          list(start = 10, end = 17, interval = 0.1),
          list(start = 18, end = 20, interval = 0.0002)
        ),
        layerId = NULL,
        group = NULL
      )
    

    enter image description here enter image description here enter image description here