HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

c f

I'm trying to use HTML5s geolocation to get the latitude and longitude of a user location and then pass those values to an ajax call.

What am I trying to do?

  1. In case of a success I would like to pass the latitude and longitude to the ajax call and in case of error, I would like to pass null.

What have I done so far?

So far, I have placed the ajax call within the success callback so that as soon as the result is available, it makes the ajax call. I would now like to wait until incase there is an error message too so that incase of an error, an ajax call can be made with null values passed.

What is happening instead?

When the user allows a location to be shared, the ajax call is made however, if the user decides not to share a location, the ajax call isnt made and instead prints out an error messages. I'm not sure how to change my code so as to listen to both success and error callbacks before making the AJAX call.

Code I have written so far:

    //error callback
    var showError = function(error) {
        var errorMessage = "Unknown Error";
        switch(error.code) {
            case error.PERMISSION_DENIED:
                errorMessage = "User denied the request for Geolocation.";
                break;
            case error.POSITION_UNAVAILABLE:
                errorMessage = "Location information is unavailable.";
                break;
            case error.TIMEOUT:
                errorMessage = "The request to get user location timed out.";
                break;
            case error.UNKNOWN_ERROR:
                errorMessage = "An unknown error occurred.";
                break;
        }
        console.log(errorMessage);

    };  


    var lat = function(callback) {
        navigator.geolocation.getCurrentPosition(function (position) {
         var lat = position.coords.latitude;
         var lon = position.coords.longitude;
         callback.call(null, lat, lon);
        }, showError);
    };

    //get the latitude and longitude
    var getPosition = function() {
        lat(function (latitude, longitude) {
         alert("lat: " + latitude + ", lon: " + longitude);
    //AJAX call made here once the latitude and longitude are returned. 
        });
    }; 

    getPosition();
Mark Vayngrib

I'm not 100% sure I understood you correctly, but you can still call the callback function on error, after you print the error for yourself, like so:

var lat = function(callback) {
    navigator.geolocation.getCurrentPosition(function (position) {
      var lat = position.coords.latitude;
      var lon = position.coords.longitude;
      callback(lat, lon);
    }, function(error) {
      showError(error);
      callback();
    });
};

var getPosition = function() {
    lat(function (latitude, longitude) {
     alert("lat: " + latitude + ", lon: " + longitude);
     // AJAX call made here once the latitude and longitude are returned. 
     // In case of error, latitude and longitude will both be undefined
    });
}; 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML5 geolocation, pass lat/long to PHP variable on click

From Dev

Geolocation - how to get city from long and lat

From Dev

How to pass $(this) in success callback of $.ajax

From Dev

HTML5 geolocation, pass lat/long to PHP variable when page load

From Dev

HTML5/Javascript GeoLocation: Pass data to callback function -or- suspend async call (with promises)?

From Dev

pass values from ajax success call

From Dev

AngularJS & Ajax post - how to get ajax error (instead of success) callback to fire

From Dev

AngularJS & Ajax post - how to get ajax error (instead of success) callback to fire

From Dev

How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

From Dev

Geolocation Using Ionic Cordova Returning Incorrect Lat/Long Values

From Dev

How to get lat and lon geolocation coordinates?

From Dev

How can I get in 'clicked' element in jquery ajax success callback

From Java

Pass variable to function in jquery AJAX success callback

From Dev

How to get lat long values of point at center of screen in WebGL Globe?

From Dev

How to get lat long after arcgis mapview scroll in android?

From Dev

How to use variable data after Ajax Call Success - Laravel

From Dev

Geolocation success/error callbacks?

From Dev

JQuery Ajax Type error in success callback

From Dev

How should be a standard AJAX call with complete, success and error section

From Dev

How to pass url to ajax success?

From Dev

PHP pass multiple parameters on success in ajax call

From Dev

Not getting success or error callback after FTP upload

From Dev

Backbone model.get('key') undefined after .fetch() call, even inside success callback

From Dev

How to pass a value to an AngularJS $http success callback

From Dev

How to pass a value to an AngularJS $http success callback

From Dev

how to pass dynamic lat long values in the javascript function from the angular js

From Dev

JQuery: How to callback each loop after completion of ajax call?

From Dev

JQuery: How to callback each loop after completion of ajax call?

From Dev

Call Javascript Library after AJAX Success

Related Related

  1. 1

    HTML5 geolocation, pass lat/long to PHP variable on click

  2. 2

    Geolocation - how to get city from long and lat

  3. 3

    How to pass $(this) in success callback of $.ajax

  4. 4

    HTML5 geolocation, pass lat/long to PHP variable when page load

  5. 5

    HTML5/Javascript GeoLocation: Pass data to callback function -or- suspend async call (with promises)?

  6. 6

    pass values from ajax success call

  7. 7

    AngularJS & Ajax post - how to get ajax error (instead of success) callback to fire

  8. 8

    AngularJS & Ajax post - how to get ajax error (instead of success) callback to fire

  9. 9

    How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

  10. 10

    Geolocation Using Ionic Cordova Returning Incorrect Lat/Long Values

  11. 11

    How to get lat and lon geolocation coordinates?

  12. 12

    How can I get in 'clicked' element in jquery ajax success callback

  13. 13

    Pass variable to function in jquery AJAX success callback

  14. 14

    How to get lat long values of point at center of screen in WebGL Globe?

  15. 15

    How to get lat long after arcgis mapview scroll in android?

  16. 16

    How to use variable data after Ajax Call Success - Laravel

  17. 17

    Geolocation success/error callbacks?

  18. 18

    JQuery Ajax Type error in success callback

  19. 19

    How should be a standard AJAX call with complete, success and error section

  20. 20

    How to pass url to ajax success?

  21. 21

    PHP pass multiple parameters on success in ajax call

  22. 22

    Not getting success or error callback after FTP upload

  23. 23

    Backbone model.get('key') undefined after .fetch() call, even inside success callback

  24. 24

    How to pass a value to an AngularJS $http success callback

  25. 25

    How to pass a value to an AngularJS $http success callback

  26. 26

    how to pass dynamic lat long values in the javascript function from the angular js

  27. 27

    JQuery: How to callback each loop after completion of ajax call?

  28. 28

    JQuery: How to callback each loop after completion of ajax call?

  29. 29

    Call Javascript Library after AJAX Success

HotTag

Archive