Python Django-用户地理定位
目前正在使用Django 1.8开发一个web应用程序,希望能够显示城市、国家和邮政编码,而无需用户输入即可注册,请有人帮我完成这项工作吗Python Django-用户地理定位,python,django,geolocation,Python,Django,Geolocation,目前正在使用Django 1.8开发一个web应用程序,希望能够显示城市、国家和邮政编码,而无需用户输入即可注册,请有人帮我完成这项工作吗 谢谢。您可以使用HTML5和谷歌地图API实现这一点 使用,您将获得用户的位置坐标 使用坐标和,您将获得用户的完整地址 示例实现- function checkLocation() { //geolocation API options var options = { maximumAge: 5 * 60 * 1000,
谢谢。您可以使用HTML5和谷歌地图API实现这一点
function checkLocation() {
//geolocation API options
var options = {
maximumAge: 5 * 60 * 1000,
enableHighAccuracy: true,
timeout: 20000};
//success getting the geolocation
function success(ppos) {
lat = ppos.coords.latitude;
lng = ppos.coords.longitude;
codeLatLng(lat, lng);
}
//error when getting the geolocation
function error(err) {
var errorMessage = "Error:";
switch(err.code) {
case err.TIMEOUT:
errorMessage = 'Error: Attempts to retrieve location timed out.';
break;
case err.POSITION_UNAVAILABLE:
errorMessage = "Error: Your browser doesn't know where you are.";
break;
case err.PERMISSION_DENIED:
errorMessage = 'Error: You have to give us permission!';
break;
case err.UNKNOWN_ERROR:
errorMessage = 'Error: Unknown error returned.';
break;
default:
errorMessage = 'Error: ' + err + ' ' + err.code;
}
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, options);
}
}
function codeLatLng(lat, lng) {
var lat = parseFloat(lat);
var lng = parseFloat(lng);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
return results[1].formatted_address;
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
我认为这个解决方案可以为您提供国家和城市,但我不认为它会为您提供邮政编码。看看,使用GeoIP时,我尝试使用GeoIP,但与私有IP@Pynchia不起作用当然不会;德威尔,我试图找到一个更普遍的解决方案@Pynchialet,如果你找到了,请告诉我!!它也可以提供邮政编码,但只能在有邮政编码的地方提供。这是大多数国家的情况。使用谷歌地理定位API的两个缺点是1。用户必须同意与您和2共享该位置。它在命名事物时有点不一致。地方性、次地方性、行政区域性、级别性对不同的国家意味着不同的东西。为了保持一致性,需要一些思考和尝试。如何在
数据库中保存位置?