Skip to content
Advertisement

R Shiny leaflet: how to query a Web Map Service layer with a fake click?

I’m trying to display the information tooltip of a queryable WMS (Web Map Service) layer in a leaflet in Shiny. I need it to be performed in 2 different ways: 1/ clicking 2/ typing in coordinates.

With the MWE (R code) at the end of this post, a click anywhere on the WMS displays the tooltip , which is part of what I want (1/). I also need the user to have the possibility to type in their coordinates (I try with “-2.55,54”), and get that same tooltip when hitting the “Go!” button (2/), without having to actually click anywhere, and I have been unable to perform this. My strategy is to fake a click when the “Go!” button is hit, by indicating what should be clicked and where (Shiny.addCustomMessageHandler('fake_a_click', function(coords){ ... has to access the leaflet map, and click where indicated in argument coords on that leaflet map). I have tried several ways of doing that:

What am I doing wrong? How can I fake a click on the leaflet map so that the WMS tooltip shows?

JavaScript

Advertisement

Answer

The problem is that you cannot easily access the map object once it is rendered. You have to store it separately, which is difficult, b/c the object is created for you by leaflet.

Good news though is that you can register an init hook, which is called whenever a new map is created. In this hook you can simply store the map object for later use. The solution is taken from this answer here: Find Leaflet map object after initialisation

Once you have a proper map object, you can use the code you provided (maybe openPopup would work as well, but I am not at all familiär with the layers provided via addWMS, so I used your original code).

JavaScript

Popup opens pers script rather than click

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement