python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.91k stars 2.22k forks source link

return lat long from mouse click #520

Closed b7j closed 7 years ago

b7j commented 8 years ago

Is it possible to return the lat and long from a mouse click over a map in folium, I have tested via latlong popover method and can see the events added as children to the map, however unable to find the actual lat long values within each child object.

BibMartin commented 8 years ago

You've got two objects for this:

features.ClickForMarker
features.LatLngPopup

I hope one of these will help you. Otherwise, can you give more precisions on the behavior you expect, please ?

b7j commented 8 years ago

okay, i have the following:

map_3 = folium.Map(location=[46.1991, -122.1889], tiles='Stamen Terrain',
                   zoom_start=13)
folium.LatLngPopup().add_to(map_3)

How do i get the actual lat and long values from the children once i have clicked on the map. I am trying to use them to start another process in notebook.

Tks

ocefpaf commented 7 years ago

How do i get the actual lat and long values from the children once i have clicked on the map. I am trying to use them to start another process in notebook.

There is no way to get the position values back as python objects right now but that would be an awesome addition :wink:

yangkey87 commented 6 years ago

Yeah, if there's such addition that would be awesome. I really need that functionality.

ocefpaf commented 6 years ago

Yeah, if there's such addition that would be awesome. I really need that functionality.

Check ipyleaft or the EarthSim project for js-python interactions. Folium is Python -> JS only.

yangkey87 commented 6 years ago

Thank you ocefpaf, didn't expect your reply so quickly. So I checked these two libraries, could not find what I want. What I really want is to showing an OSM tile, with the function to let user add a marker using mouse. Then maybe there's a confirm mechanism to ask user to confirm the marker's location. After confirm, my map will store the marker's lat n long values to a database. Do you think this is too much for a simple visualization tool? Maybe I'll stick to Geodjango? Thanks

dgoines commented 5 years ago

yangkey87, do you have a solution to what you're trying to do? I'm looking to do something similar and have not been able to figure out how to store the user-clicked lat/lon data. Thanks.

yterletskyi commented 5 years ago

I've ended up by saving the map to an html file on disk and editing javascript part there. This way I'm able to get corrdinates using e.latlng inside function latLngPop(e) {...}. However it restricts you to work with that coordinates in the map.html, not in initial python script.

Aveen1 commented 4 years ago

Thank you ocefpaf, didn't expect your reply so quickly. So I checked these two libraries, could not find what I want. What I really want is to showing an OSM tile, with the function to let user add a marker using mouse. Then maybe there's a confirm mechanism to ask user to confirm the marker's location. After confirm, my map will store the marker's lat n long values to a database. Do you think this is too much for a simple visualization tool? Maybe I'll stick to Geodjango? Thanks

I'm finding the same thing, anyone know how to do it?

Conengmo commented 4 years ago

folium really is not build for this kind of stuff. It's a one way street from Python to html/JS.

One way of solving this I can think of is making a webapp. You'll have to do some web dev kind of work yourself. Something like a Flask webapp showing maps. Add custom Javascript that sends the coordinates to a URL of your Flask webapp. That way you have the coordinates in Python and can store them in a db or whatever it is you want.

Here's an example to get started with folium and Flask: https://python-visualization.github.io/folium/flask.html After that you're on your own I'm afraid. Though if you make a small demo we would gladly host it in our example gallery.

Aveen1 commented 4 years ago

folium really is not build for this kind of stuff. It's a one way street from Python to html/JS.

One way of solving this I can think of is making a webapp. You'll have to do some web dev kind of work yourself. Something like a Flask webapp showing maps. Add custom Javascript that sends the coordinates to a URL of your Flask webapp. That way you have the coordinates in Python and can store them in a db or whatever it is you want.

Here's an example to get started with folium and Flask: https://python-visualization.github.io/folium/flask.html After that you're on your own I'm afraid. Though if you make a small demo we would gladly host it in our example gallery.

Okay I'll let you know if it works😄

hasantanvir79 commented 4 years ago

I am stuck with the same issue! Is there any update on this thread? thanks :)

xetyrj22 commented 4 years ago

In my case I use folium plugin "Draw" for set markers and framework PyQt5. Everything work fine.

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from folium.plugins import Draw
import folium, io, sys, json

if __name__ == '__main__': 
    app = QtWidgets.QApplication(sys.argv)

    m = folium.Map(location=[55.8527, 37.5689], zoom_start=13)

    draw = Draw(
        draw_options={
            'polyline':False,
            'rectangle':True,
            'polygon':True,
            'circle':False,
            'marker':True,
            'circlemarker':False},
        edit_options={'edit':False})
    m.add_child(draw)

    data = io.BytesIO()
    m.save(data, close_file=False)

    class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
       def javaScriptConsoleMessage(self, level, msg, line, sourceID):
          coords_dict = json.loads(msg)
          coords = coords_dict['geometry']['coordinates'][0]
          print(coords)

  view = QtWebEngineWidgets.QWebEngineView()
  page = WebEnginePage(view)
  view.setPage(page)
  view.setHtml(data.getvalue().decode())
  view.show()
  sys.exit(app.exec_())

For disable alarm you need edit folium lib (../../foluim/plugins/draw,py) remove line 63 -

  alert(coords); 
gg-bt commented 3 years ago

@xetyrj22 , after some time I would bother you for a simple question... how does your code exactly work? I am quite a newbie, sorry about that.

Nbandhi commented 3 years ago

I am using folium map inside my flak application. The map is to help user select a neighbor-hood to further explore, I need the selected lat and lon passed back to my flask app. I have tried by providing a html popup for each marker with a form to submit the lat and lon through hidden fields. It did not work. Would love to understand how I can pass the selected neighbor-hood's lat and lon back to my flask apps route.

osvenledezmachomon commented 3 years ago

@Nbandhi , @xetyrj22 hi hi how are u ? I have this to get the information from the mobile phone

from folium.plugins import LocateControl m = folium.Map()

"""With default settings""" LocateControl().Add_to(m) and I need to know the latiud how i can used your code?

mejl commented 2 years ago

How do you save the LatLng to a variable in python, when you click the folium map?

BibMartin commented 2 years ago

When you click the folium map, you need that something catches the "event" in your browser and send the information back to python (they are two separate processes).

If you run jupyter on your local laptop, you may use the clipboard to do so : (see #1530)

VictorCubas commented 1 year ago

In my case I use folium plugin "Draw" for set markers and framework PyQt5. Everything work fine.

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from folium.plugins import Draw
import folium, io, sys, json

if __name__ == '__main__': 
    app = QtWidgets.QApplication(sys.argv)

    m = folium.Map(location=[55.8527, 37.5689], zoom_start=13)

    draw = Draw(
        draw_options={
            'polyline':False,
            'rectangle':True,
            'polygon':True,
            'circle':False,
            'marker':True,
            'circlemarker':False},
        edit_options={'edit':False})
    m.add_child(draw)

    data = io.BytesIO()
    m.save(data, close_file=False)

    class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
       def javaScriptConsoleMessage(self, level, msg, line, sourceID):
          coords_dict = json.loads(msg)
          coords = coords_dict['geometry']['coordinates'][0]
          print(coords)

  view = QtWebEngineWidgets.QWebEngineView()
  page = WebEnginePage(view)
  view.setPage(page)
  view.setHtml(data.getvalue().decode())
  view.show()
  sys.exit(app.exec_())

For disable alarm you need edit folium lib (../../foluim/plugins/draw,py) remove line 63 -

  alert(coords); 

is there any way to disable without touching that file? (draw.py)

VictorCubas commented 1 year ago

to disable alerts by clicking on markers

Conengmo commented 1 year ago

The Draw plugin has an argument show_geometry_on_click that can be used to disable the alert of the coordinates. https://github.com/python-visualization/folium/blob/main/folium/plugins/draw.py#L20

Draw(show_geometry_on_click=False)