如何将 google 个位置保存到 Django 模型?

How do I save google places location to django models?

我正在尝试创建一个高度依赖位置的应用程序。我正在使用 google maps places api 来允许用户 select 一个位置并将其添加到数据库中。我目前的问题是,我似乎无法弄清楚如何将 google 地图位置 JavaScript api 中的位置保存到数据库中。例如,Suzy 选择了一家商店,并希望将该商店及其位置保存到数据库中。我似乎无法弄清楚如何将商店位置保存到数据库中。知道如何做到这一点吗?

一种方法是保存经度和纬度,因为它们可以表示全球的确切位置,您可以添加类似

的字段
long = DecimalField(max_digits=9, decimal_places=6)
lat  = DecimalField(max_digits=9, decimal_places=6)

因为小数点在坐标中很重要,但是使用6以上基本没有意义。

通过geolocation()获取经纬度see this

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, make more fields like street address, city in db and pass it to inputs and submit the form to save it.
          });
        }