Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get add_draw polygon coordinates back #252

Open
orgoca opened this issue Jun 5, 2022 · 2 comments
Open

Get add_draw polygon coordinates back #252

orgoca opened this issue Jun 5, 2022 · 2 comments

Comments

@orgoca
Copy link

orgoca commented Jun 5, 2022

I'd like to know if you have any recommendation for obtaining the polygon coordinates back from the draw polygon method. So far, I am able to get the user to draw a polygon but I cant seem to find a method to retrieve the polygon coordinates (lat long) back from the map object created.

Ideally I would like to get is a list of lat-long-lists for each point the user selected in their polygon. For a 12 sided polygon I would have 12 lat-long lists inside a list, etc.

My use case is a Shiny UI in which the user can select a polygon, and then I'd use this data (lat-long list of lists) to send via a Fast API for image retrieval, inference methods and returning analytics to Shiny.

Is this possible to do and if so which method should I use?

So far I tried this to no success:

userPolygon <- google_map(location = c(34.056458, -118.247131),
                              zoom = 10,
                              # split_view = "pano",
                              event_return_type = 'list')
            
            userPolygon <- add_drawing(
                userPolygon ,
                drawing_modes = c("polygon"),
                delete_on_change = FALSE
            )
            
            results <- access_result(
                userPolygon ,
                result = c("points")
            )
            
            View(results)
@orgoca
Copy link
Author

orgoca commented Jun 5, 2022

For reference, here is a Javascript implementation of the same idea. If you try the code snipet at the bottom, after drawing a polygon, you'll get a tupple of tupples containing all lat-long pairs that create the polygon. This is nice but not useful for me as I am doing my development on Shiny and not on Javascript.

https://stackoverflow.com/questions/27679291/drawing-polygon-in-google-map-using-javascript/27688473#27688473

@dcooley
Copy link
Collaborator

dcooley commented Jun 5, 2022

Thanks for the question. And the answer is straight forward: You simply need to observe the _polygoncomplete shiny method.

Here's a minimal example. After drawing the polygon you'll see the data returned to your R session (printed in the console)

library(shiny)
library(googleway)

set_key(secret::get_secret("GOOGLE"))

ui <- fluidPage(
  google_mapOutput('myMap')
)

server <- function(input, output){
  
  output$myMap <- renderGoogle_map({
    google_map(event_return_type = "list") %>%
      add_drawing()
  })
  
  observeEvent(input$myMap_map_click, {
    print(input$myMap_map_click)
  })
  
  observeEvent(input$myMap_polygoncomplete, {
    print(input$myMap_polygoncomplete)
  })
  
}

shinyApp(ui, server)

Reference:

  • Vignette article
  • you can see the method implemented here in drawing.js - as well as similar methods for _circlecomplete, _markercomplete etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants