kurrodu
kurrodu

Reputation: 2188

How to pass a geolocation variable from client to server side

Can someone please suggest me a method to pass a client side Java Script location variable (lat,lng) to my server side as soon as the user allows for his geolocation to be used. So that I can use the location variable to query my database for stores within a range of 10 km and display them on a map.

I am using HTML5 Geolocation on the client side for obtaining user's location and Django + PostGIS database on the server side for carrying out the distance lookups.

Upvotes: 0

Views: 3020

Answers (1)

kurrodu
kurrodu

Reputation: 2188

Inorder to pass the user Geolocation from client side to server side as soon as the user allows for his Geolocation to be used, is by

  1. Assigning user position variable (pos) to a HTML element (location)
  2. Then, auto submitting a HTML form that contains a POST method.

Both the above codes have to be written within Google Geolocation API, as shown below in the excerpt.

var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });
  // the below line has been inserted to assign a JS variable to HTML input field called 'geolocation'
document.getElementById('location').value = pos
  map.setCenter(pos);
// the below line has been inserted to autosubmit the form.  
document.getElementById("geolocation").submit(); 

Below is the HTML form

<form id = "geolocation" action="/location" method="POST" >
            {% csrf_token %}
        <input type="text" id = "location" name="location" value="" />
        <input type="submit" />

</form>

Upvotes: 2

Related Questions