简体   繁体   中英

Plot severals markers on a leaflet map

Hi i'm trying to learn how to use a leaflet map on shiny, I used that example :

http://glimmer.rstudio.com/jcheng/leaflet-demo/

Here is the code repository :

https://github.com/jcheng5/leaflet-shiny/blob/master/inst/example/

I'm would like to replace circles by markers by replacing the addCircle function by addMarker .

The actual function is : (line 98 of Server.R)

map$addCircle(
  cities$Lat,
  cities$Long,
  sqrt(cities[[popCol()]]) * radiusFactor / max(5, input$map_zoom)^2,
  row.names(cities),
  list(
    weight=1.2,
    fill=TRUE,
    color='#4A9'
  )
)

And I just replaced it by :

map$addMarker(
  cities$Lat,
  cities$Long,
  row.names(cities)
)

But it only plot the marker on the first city of the data frame. And after if you move and zoom randomly on the map some other markers can appear...

Why addCircle draws a circle for each cities and addMarker behaves "randomly" ?

How can I draw severals marker at once on the plot ?
The loop bellow works but ideally I don't want to loop manually if it's possible.

for(i in 1:nrow(cities)){
    map$addMarker(
      cities$Lat[i],
      cities$Long[i],
      i
    )
}

Thank you

I had the same problem, it is because you remove the radius. By looking at the code for the function createLeafletMap, we can see that addCircle needs these arguments:

addCircle(lat, lng, radius, layerId = NULL, options=list(), eachOptions=list())

it really need radius. to solve your problem just write:

map$addMarker(
  cities$Lat,
  cities$Long,
  100,
  row.names(cities)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM