Saving Json data from javascript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving Json data from javascript

Shazia Nusrat
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);
     
}

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Saving Json data from javascript

Etienne Robillard-6

Hi Shazia,

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

HTH,

Etienne


Le 2018-02-12 à 18:04, Shazia Nusrat a écrit :
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);
      }

--
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 [hidden email].
To post to this group, send email to [hidden email].
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
[hidden email]
https://www.isotopesoftware.ca/

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/e87c4a18-519e-5264-00cb-65d3203048bd%40yandex.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Saving Json data from javascript

Shazia Nusrat
Can you please show me any example about the structure of code I've provided?

Thanks

On Tue, Feb 13, 2018 at 2:27 AM, Etienne Robillard <[hidden email]> wrote:

Hi Shazia,

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

HTH,

Etienne


Le 2018-02-12 à 18:04, Shazia Nusrat a écrit :
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);
      }

--
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 [hidden email].
To post to this group, send email to [hidden email].
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
[hidden email]
https://www.isotopesoftware.ca/

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAD83tOyZn16gV7%3D2am9W8je-Q4mQALNNR5TAgPwYvVs%2B_n%2B9aQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Saving Json data from javascript

Etienne Robillard-6

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

Le 2018-02-13 à 08:34, Shazia Nusrat a écrit :
Can you please show me any example about the structure of code I've provided?

Thanks

On Tue, Feb 13, 2018 at 2:27 AM, Etienne Robillard <[hidden email]> wrote:

Hi Shazia,

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

HTH,

Etienne


Le 2018-02-12 à 18:04, Shazia Nusrat a écrit :
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);
      }

--
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 [hidden email].
To post to this group, send email to [hidden email].
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
[hidden email]
https://www.isotopesoftware.ca/


-- 
Etienne Robillard
[hidden email]
https://www.isotopesoftware.ca/

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/42a3efaf-7a67-b436-d297-be9d3dfcdad7%40yandex.com.
For more options, visit https://groups.google.com/d/optout.