Saving Json data from javascript

45 views
Skip to first unread message

Shazia Nusrat

unread,
Feb 12, 2018, 6:06:49 PM2/12/18
to django...@googlegroups.com
Hi,

Below is the code for Javascript from Django template Google Maps API. I need to be able to save this in my Django models with JSON response. I am new user so an example will be really a great help.

Precisely I need to be able to save coordinates like latitude and longitude and then convert it to an address.

Thanks 
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);
     
}

Etienne Robillard

unread,
Feb 13, 2018, 5:29:05 AM2/13/18
to Shazia Nusrat, django...@googlegroups.com

Hi Shazia,

You can make a XHR (Ajax) request in javascript to send the data to a django callback.

HTH,

Etienne

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD83tOzabkiPgniqofN6BH2tLogeiGE9JaZ5rppi3f44Hb0ybA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Etienne Robillard
tka...@yandex.com
https://www.isotopesoftware.ca/

Shazia Nusrat

unread,
Feb 13, 2018, 8:35:57 AM2/13/18
to Etienne Robillard, django...@googlegroups.com
Can you please show me any example about the structure of code I've provided?

Thanks

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

Etienne Robillard

unread,
Feb 13, 2018, 8:40:39 AM2/13/18
to Shazia Nusrat, django...@googlegroups.com

I'm not really sure what you meant here. Anyways, there's plenty of good JS frameworks like jQuery to allow you to write Ajax functions to send a JSON array for post-processing in Django.

Etienne
Reply all
Reply to author
Forward
0 new messages