Rick Arko
Rick Arko

Reputation: 680

How to specify radius units in addCircleMarkers() when using Leaflet in R

I'm trying to visualize a shipment from origin to destination using a leaflet map created with R within a shiny application.

I want to add a circle marker of a radius corresponding to a the odist and ddist variables which come form a reactive dataframe called main()

below is a relevant snapshot with accompanying code:

Pic1

output$leaflet1 <- renderLeaflet({
  leaflet() %>%
    addCircleMarkers(data = main(), lng = main()$Olong, lat = main()$Olat, color = 'black', fillColor = coyGreen,
                     radius = main()$odist, opacity = .5) %>% 
    addCircleMarkers(data = main(), lng = main()$Dlong, lat = main()$Dlat, color = 'black', fillColor = coyGreen, 
                     radius = main()$ddist, opacity = .3)
})

For the above example the argument radius = main()$odist is equivalent to radius = 50. However, the 50 units seem to be arbitrary (the cirlce is smaller than the larger one with radius = main()$ddist = 125 however both circles enlarge and shrink as I zoom in and out). I would like to be able to set the radius of my circle marker to be a fixed radius in miles, however I haven't been able to figure out how to do so. Any help is greatly appreciated!

Upvotes: 4

Views: 12423

Answers (2)

Wolkuz
Wolkuz

Reputation: 83

It is important to remember the map projections.

It also depends on the projection you are using in Leaflet, it is good to check it with Leaflet's measurement tools or if you know the location well. To see the projections I recommend

crsDF <- rgdal::make_EPSG()
View(crsDF)

In my case, that I am in Chile (a long and narrow country) I use this projection for some places:

CRS 9155 =

"+proj=utm +zone=19 +south +ellps=GRS80 +units=m +no_defs +type=crs"

There you can see that I am using units in meters

Here I leave an image of some projections for my country.

View(crsDF)

View(crsDF)

You can find more in:

Greetings

Upvotes: 0

TimSalabim
TimSalabim

Reputation: 5834

If you use addCircles instead of addCircleMarkers your circles will have constant radius (in meters). Here's a reproducible example using mapview which uses addCircleMarkers. On top we plot the same locations using addCircles

library(mapview)

m <- mapview(breweries91) # uses addCirclemarkers so circle radius changes with zoom

m@map %>% 
  addCircles(data = breweries91, color = "red", radius = 100) # circle radius constant

If you zoom in you will see that the initially smaller red circles will become bigger in relation to the standard blue circlemarkers used in mapview

Upvotes: 9

Related Questions