I am new to Django and I am just trying to figure it out and I did posted it in official Django google group but no answer. I am working with Google Map API in my Django template for GeoLocation and I intend to populate DB table dynamically after extracting information from JavaScript in my Django template. Following is the code:
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
I need to save longitude and latitude with GeoLocation Info about the location in human readable format in my model. My Model is
class Address(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)
latitude = models.CharField(max_length=10)
longitude = models.CharField(max_length=10)
address = models.CharField(max_length=100)
.....
My form is:
class UserAddressForm(forms.ModelForm):
class Meta:
model = Address
fields = ['latitude', 'longitude', 'address'}
I need to figure it out in my view how to do it. It will be great if someone can refer me to right direction because I am really having hard time to figure it out as newbie.