How to retrieve the coordinates on-click and save it as a variable? #1213
-
Hi, I like to retrieve the coordinates when I click on a map and save them in a variable for further processing. There is an example provided by geemap (here):
This shows the coordinates on the map but there are two issues, I don't know how to save the coordinates in a variable that I can use later, and secondly, I cannot move the map since when I click and hold the mouse to move the map, it only returns the coordinates. I wonder is there a way to save and update a variable that contains the on-click coordinates extracted from the above code? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can create an attribute for the Map (.e.g., import ee
import geemap
import ipywidgets as widgets
from ipyleaflet import WidgetControl
Map = geemap.Map()
Map.points = []
# Add an output widget to the map
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
# Capture user interaction with the map
def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')
if kwargs.get('type') == 'click':
Map.default_style = {'cursor': 'wait'}
# xy = ee.Geometry.Point(latlon[::-1])
with output_widget:
output_widget.clear_output()
print(latlon)
Map.points.append(latlon)
Map.default_style = {'cursor': 'pointer'}
Map.on_interaction(handle_interaction)
Map Map.points |
Beta Was this translation helpful? Give feedback.
-
Just to add to this, I recently needed to do something similar, and realised that the code above does as intended, but it will append a point for each interaction with the map, so just moving the mouse around will trigger points being added to the by rearranging the code a bit you can get it to only save the points: import ee
import geemap
import ipywidgets as widgets
from ipyleaflet import WidgetControl
Map = geemap.Map()
Map.points = []
# Add an output widget to the map
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
# Capture user interaction with the map
def handle_interaction(**kwargs):
if kwargs.get('type') == 'click':
latlon = kwargs.get('coordinates')
Map.default_style = {'cursor': 'wait'}
with output_widget:
output_widget.clear_output()
print(latlon)
Map.points.append(latlon)
Map.default_style = {'cursor': 'pointer'}
Map.on_interaction(handle_interaction)
Map Dropping it here in case it helps somebody in the future! |
Beta Was this translation helpful? Give feedback.
You can create an attribute for the Map (.e.g.,
Map.points = []
), and append points to the list with mouse click events. Then retrieve the points usingMap.points