in googleway package how to define id of a polygon

27 views Asked by At

I have this shiny R server code that I am trying to work:


vertices <-
structure(list(lat = c(20.99, 20.99, 21.01, 21.01, 20.99), lng = c(83.99, 
84.01, 84.01, 83.99, 83.99)), class = "data.frame", row.names = c(NA, 
-5L))

server <- function(input, output, session) {
 
  output$map <- renderGoogle_map({
    google_map(key = apikey,
               map_type = "satellite",
               location = c(21.92, 85.43),
               zoom = 14
               ) %>%
      add_polygons(vertices,lat = "lat", lon = "lng",editable = T)
  })

  observeEvent(input$map_polygon_edited, {
    edited_data <- input$map_polygon_edited
    print(edited_data)
  })
}

While I can see the google map and a rectangle with handles on the vertices that are draggable, I donot get the observer reactive input$map_polygon_edited working.

i have tried adding an id parameter in add_polygons() but am not sure how that would work as the googleway documentations says that the id has to be a column name. So what exactly do we pass as map_id in the observer event? The column name or the map output name or the row content?

1

There are 1 answers

2
Stéphane Laurent On

The doc is not very clear but there's an example in the file manual_tests.R.

In fact, the editable argument must be the name of a TRUE/FALSE column of the data which indicates whether the polygon defined at each row is editable or not.

So, firstly, your dataframe vertices is not appropriate. You firstly have to construct a polyline which represents this polygon, with the help of the googlePolylines package. Then, make a one-row dataframe with this polyline in a column and with another column containing TRUE to indicate that the polygon represented by this polyline is editable:

polygon <- data.frame(
  polyline = googlePolylines::encodeCoordinates(vertices$lng, vertices$lat),
  editable = TRUE
)

then:

output$map <- renderGoogle_map({
  google_map(key = apikey,
             map_type = "satellite",
             location = c(21.92, 85.43),
             zoom = 14
  ) %>%
    add_polygons(
      polygon, polyline = "polyline", editable = "editable"
    )
})

I didn't test because I don't have an API key.