如何使用 google 地图 api 在 django 中获取用户位置?
How to use google maps api to get user location in django?
我正在做一个 django 项目,需要获取用户位置,我尝试的是用户可以手动输入他的城市,或者单击定位按钮,它会自动找到它的位置并填充城市。
我尝试了 GeoIP 但还不够,是否可以使用 Google 地图 api?
如果不是,那么某些网站如何自动填充我的位置,例如 - 电影票预订网站、酒店预订网站?
如果你能告诉我一个可行的解决方案,我想要实现的是什么?
我使用 GeoLocation 获取经度和纬度,然后使用 google api 我获取街道地址、城市、州、国家等。
这是我使用的代码 -
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.
});
}
我正在做一个 django 项目,需要获取用户位置,我尝试的是用户可以手动输入他的城市,或者单击定位按钮,它会自动找到它的位置并填充城市。
我尝试了 GeoIP 但还不够,是否可以使用 Google 地图 api?
如果不是,那么某些网站如何自动填充我的位置,例如 - 电影票预订网站、酒店预订网站?
如果你能告诉我一个可行的解决方案,我想要实现的是什么?
我使用 GeoLocation 获取经度和纬度,然后使用 google api 我获取街道地址、城市、州、国家等。
这是我使用的代码 -
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.
});
}