Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So if i want to pull out lat/lngs after user clicks on a map canvas (lat/lngs are pulled using any javascript API (leaflet, wax etc.) and add this to a postgresql database using a query, how do i do this?

I came across:

But how do you generally add values to a db on the fly?

share|improve this question

I might be getting this wrong, but just to check, you aren't planning on using on a client/user-facing website are you? It looks like it connects with a password provided, which means you'd be supplying the user with your db password!

All you need to store points that the user clicks is a backend that can insert data as Aragon said, and a simple PUT to e.g. a flask app -

Front end:

map = L.Map(...);
map.on('click', function(e) {
    $.ajax('someurl/putpoint/', {
      type: 'PUT',
      data: {
        lng: e.latlng.lng

Back end with e.g. :

from flask import Flask
app = Flask(__name__)

def putpoint():
    cursor = get_your_db_cursor()
    # NB: massively insecure accepting any random input from PUT!
    # Should use the params arg in execute() rather than format str
        INSERT INTO test(p_id, the_geom)
        VALUES(2, ST_GeomFromText('POINT(%s %s)', 4326));""" % (
        request.form['lng'], request.form['lat']))  

if __name__ == '__main__':
share|improve this answer

did you implement this insert function to js library you give?

PostGIS adding geomerty function:

 INSERT INTO test(p_id, the_geom)
 VALUES(2, ST_GeomFromText('POINT(10 20)', 4326));

Your postgres backend protocol insert function:

 db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id, val", function (sth) {
 sth.execute("text value", function(e, rs) {
 if (rs === undefined) {
 console.log("No data.");
 else {

i hope it helps you...

share|improve this answer

To add values to a datebase on the fly, break it up in three steps:

1) Catch the click and position in Javascript:

2) Send the position to a server using Ajax:

3) Save the values in the database using PHP:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.