-1

I have a form, which is submmited via the malsup ajaxForm plug-in, and I want the ajax call to wait until leaflet accurate position plug-in finds an accurate position, so then I can append it to the data sent by the form setting it in some hidden fields I have in my html. The thing is, I don't really have a clear way of how to do this.

I have the following:

function InitMap()
{
map = L.map('map');

function onAccuratePositionError (e) {
    addStatus(e.message, 'error');
}

function onAccuratePositionProgress (e) {

}

function onAccuratePositionFound (e) {
    $('#latitud').val(e.latlng.lat);
    $('#longitud').val(e.latlng.lng);
    $('#presicion').val(e.accuracy);
}

map.on('accuratepositionprogress', onAccuratePositionProgress);
map.on('accuratepositionfound', onAccuratePositionFound);
map.on('accuratepositionerror', onAccuratePositionError);

}

And my Ajax call:

function InitJForm()
{
  $('form').ajaxForm({
        beforeSubmit:Geolocalizar,
        success: function(datos)
         { 
            window.location.href = 'some url...';
         },
        error: function(datos) 
        {
        var w = window.open('about:blank');
            w.document.open();
            w.document.write(datos.responseText);
            w.document.close();
    }
   });
}

The geolocation call:

function Geolocalizar()
{
map.findAccuratePosition({
    maxWait: 15000,
    desiredAccuracy: 20
});
return true;
}

Is there a way to make the geolocation call to wait for the findaccurateposition function to finish finding the location before sending a true and submitting the request?

j1rjacob
  • 411
  • 11
  • 27
Leandro Gamarra
  • 141
  • 2
  • 12

2 Answers2

1

Well.. after a lot of work and trying Defferred object with no succes too, (couldnt catch when Api function "findAccuratePosition" finished because I think it finishes inmideately)...I decided to remove MALSUP ajaxForm plugin and make a simple custom $.ajax. post.

I prevent the default submit and call e.preventdefault. I just moved the custom ajax request to the OnAccuratePosition found,so Im assured that, after geolocation finds the coords, The ajax post is executed with the location info. Also my form was submitting some Files (imgs), and had to come across the FormData object solution, to upload Files via AJAX, wich I only found HERE beautifully explained:

http://hayageek.com/jquery-ajax-form-submit/

Leandro Gamarra
  • 141
  • 2
  • 12
0

The vanilla ajax call have a property async:true or async:false. This you could leverage in your current code by other means(you can definitely figure it out)

There is a relevant community question which could help you-

What does "async: false" do in jQuery.ajax()?

Update: now As you said, you need to wait for a function to finish. You could do that by passing callback functions as the arguments. First the calllback functions are executed and only then the funtion into which these are passed as parameter are executed. Below is an outline on how you can use callbacks in your function.

alert(Geolocalizar(function(){
                          map.findAccuratePosition({
                          maxWait: 15000,
                          desiredAccuracy: 20  });
                          return true;    
                             }
                  )
);

function Geolocalizar(callback)
    {
    // this code will be executed after callback function is executed
    // print or return anything;
    }
anubhs
  • 566
  • 1
  • 8
  • 26
  • I will definetly try it out! – Leandro Gamarra Feb 05 '19 at 20:44
  • I have read a little about what you told me, and I dont think Its going to help me. The geolocation call could at least last 15 seconds, I want to wait the method "findAccuratePosition" to finish, and only then return true, so that the Ajax Call is made. – Leandro Gamarra Feb 06 '19 at 12:02
  • Please do not recommend using `async: false` in jQuery's `$.ajax()` function: that is officially deprecated as of v1.8: http://api.jquery.com/jquery.ajax/ – Terry Feb 06 '19 at 13:02
  • I have tried the above but it seems that "map.findaccurateposition" executes and finishes before "onAccuratePositionFound" is fired and sets the values... I may have to use a timer or of the sorts? :S – Leandro Gamarra Feb 06 '19 at 14:06