我正在做一个django项目,需要获得用户位置,我尝试的是用户可以手动进入他的城市,或者点击location按钮,自动找到它的位置并填充这个城市。
我试过GeoIP,但还不够,用Google可以吗?
如果没有,那么有些网站如何自动填充我的位置,比如-电影票预订,酒店预订网站?
如果你能告诉我一个可行的解决方案,我想实现的是什么?
发布于 2019-08-01 13:11:57
我使用GeoLocation获取经度和纬度,然后使用google获取街道地址、城市、州、国家等信息。
这是我用的密码-
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.
});
}https://stackoverflow.com/questions/53638310
复制相似问题