Skip to content

Commit

Permalink
Added map listening function
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Mar 10, 2020
1 parent 670f805 commit 24f8345
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 8 deletions.
96 changes: 88 additions & 8 deletions examples/template.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "52f3f95c6d8c42f2989cfca1fda19b41",
"model_id": "0e4633b38a204fde9a92189750d15c65",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -210,7 +210,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2d7a5832df964ccc80313222d5b5d399",
"model_id": "6e6d1cb853654442b904479bab917e2a",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -224,7 +224,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "69ce5d2c3d1c4ce5b4b420f6ccd33893",
"model_id": "ed6bdb6285ac4b98b080e72159e97fc9",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -263,14 +263,14 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[36.95208671786997, -106.95516114945306], [40.78262115769853, -84.89031816016386], [46.19028890427803, -108.09796178236444]]\n"
"[]\n"
]
}
],
Expand All @@ -293,7 +293,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6ac9daec1eb943358f1c204af1e7d57f",
"model_id": "46c6d1c32b26403998753c23a0054e75",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -307,7 +307,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "18f395fa020044ca843b2b324d340b23",
"model_id": "efabed93955d427ba6644e1b8d2eccec",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -321,7 +321,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4cfc7fea73c94e05a2cf9e89fcd2af50",
"model_id": "ebc2d97a64644ff9bde4718f2124c28a",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -373,6 +373,86 @@
"Map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A simplifed version for capturing user interaction"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a292e5d857e34ac3a46f8f2c57712e96",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[40, -100], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_t…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import geemap\n",
"Map = geemap.Map(center=(40, -100), zoom=4)\n",
"Map.listening(event='click', add_marker=True)\n",
"Map"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[40.40617642341373, -113.46033398294868]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get the last mouse clicked coordinates\n",
"Map.last_click"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[43.859286990143055, -106.95516114945306],\n",
" [42.12369172732473, -93.15364581352316],\n",
" [36.20106624342554, -93.15364581352316],\n",
" [34.914088616906106, -102.64768184078703],\n",
" [40.40617642341373, -113.46033398294868]]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get all the mouse clicked coordinates\n",
"Map.all_clicks"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
23 changes: 23 additions & 0 deletions geemap/geemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ def addTileLayer(self, url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
ipyleaflet.Map.addTileLayer = addTileLayer


def listening(self, event='click', add_marker=True):

coordinates = []

def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')

if event == 'click' and kwargs.get('type') == 'click':
coordinates.append(latlon)
self.last_click = latlon
self.all_clicks = coordinates
if add_marker:
self.add_layer(Marker(location=latlon))
elif kwargs.get('type') == 'mousemove':
pass

self.on_interaction(handle_interaction)

ipyleaflet.Map.listening = listening
ipyleaflet.Map.last_click = []
ipyleaflet.Map.all_clicks = []


if __name__ == '__main__':

map = Map()
Expand Down

0 comments on commit 24f8345

Please sign in to comment.