0

On a webpage with leaflet maps - I want to be able to display my database objects, each of which have a lat,lng. using markers on the map.

So when I click the marker, I would like to fetch information from db and display this.

How can I store the object id or associate it with a marker so that when a marker is clicked I can load my object from database and display detailed information?

I know I could use lat/lng to find the object using radius search or something, but this won't work and is not accurate, as there could be more than one object with same lat/lng.

srinivas
  • 4,778
  • 2
  • 32
  • 43

2 Answers2

3

Ok based on the answer provided by @Ivar in another thread: Leaflet: Add a link to the markers

You can associate a dbObject Id to the marker directly, after the marker is added to the map. when a marker is added to the map it gets assigned an ID called "_leaflet_id". This can be fetched through the target object, and also set to a custom value after it has been added to the map.

Now in the marker OnClick event you can just get the clicked marker's id and use it to query the database.

marker._leaflet_id = dbObjectId;
Community
  • 1
  • 1
srinivas
  • 4,778
  • 2
  • 32
  • 43
1

Not sure how you are connecting to your db, but the flow would be something like this:
grab the clicked point's lat/lng, then send that info to some logic that employs a spatial geometry lib like S2 or something. Create an envelope around the clicked point, and get all the points whose lat/lngs are within that envelope, then whichever's lat/lng is the shortest spherical distance to your clicked point is your match.
I am sure there is an easier way to do this through CartoDB or something like PostGIS.

--
Assuming your database objects are set up in a table like this:

ID(guid) | Name | Latitude | Longitude
fe124etc.| Marker1| 43.123 | -117.123
y75W2etc.| Marker2| 44.123 | -116.123

you do the method above and then once you have the marker you want, you send the other db data (name, address, w/e) back to a popup that is bound to the marker.


Another avenue to explore is (depending on the data) you can make a geoJSON layer out of it. (leaflet geojson use). If you do that, you can create/store/read attributes for each point.
Hope this makes it a little more clear.

Ju66ernaut
  • 2,592
  • 3
  • 23
  • 36
  • Hi, I understand this method. My question is particular to leaftlet maps, for implementation on any website. I know that I can search and get all the markers for a particular lat/lng radius. But this does not solve the problem, as clicking on a marker will return me multiple rows which have the same lat/lng values. How would I be able to individually identify each marker? Unless I specifically implement a HahMap or something to store individual marker and its associated db object. – srinivas Mar 18 '14 at 19:50