2

I am working on a django project and need to get the user location, what I trying is either user can enter his city manually or click on locate button that will automatically finds its location and fill the city.

I tried GeoIP but it is not sufficient, is it possible with Google maps api ?

If not then how some websites automatically fills my location like - movie ticket booking, hotel booking websites ?

If you can tell me a feasible solution of what I want to achieve is ?

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
  • I think you can find there what you are looking for: https://stackoverflow.com/questions/2218093/django-retrieve-ip-location – Sergey Pugach Dec 06 '18 at 16:16
  • @SergeyPugach Thanks for reply but as already mentioned that GeoIp is not sufficient for my work, any other suggestions? – Pankaj Sharma Dec 07 '18 at 11:25
  • @PankajSharma Did you ever find a solution? I'm just getting started with Django so I might be missing something, but there's practically no documentation, no tutorial on how to get it done. – Collins Orlando Aug 01 '19 at 13:07
  • @CollinsOrlando I have posted the answer that I used, hope it will help you :) – Pankaj Sharma Aug 01 '19 at 13:12

1 Answers1

1

I used GeoLocation to get the longitude and latitude, then with google api I fetch the street-address,city, state, country, etc.

here is the code that I used -

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {

         let lat = position.coords.latitude;
         let long = position.coords.longitude;
         $("input[name='lat']").val(lat);
         $("input[name='lat']").val(long);
         let url_str = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&key=yourkey'
        $.getJSON(url_str, function(data) {
              console.log(data);
              //here you get the location data, like street address, city and pass it to inputs and submit the form to save it.
          });
        }
Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50