1

I have the following code which gets the geo location and posts it, then displays a button in div. This works on desktops but does not work in android a common issue as mentioned on this site. Recommendations are to add

 {frequency:5000, maximumAge: 0, timeout: 100, enableHighAccuracy:true} 

to the code below somewhere, I've tried a few places just stops it working, where do I put these values please?

function getCoordPosition() { 

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
    $.ajax({
        type: "POST",
        url: "{{path('login_log_location')}}",
           data: {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude
        },
        success: function () {
            $("#divputinarea").html("{{path('login_log_yourarea')}}">
           <button class="btn btn-large btn-primary">
           Find </button>');
          }

           });   
       });
    }  

}

After advice below I tried the following, it works without the { maximumAge: 0,timeout: 100,enableHighAccuracy:true} if I add it I get error 'argument 2 of geolocation.getCurrentPosition not callable"

 function getCoordPosition() { 

  if (navigator.geolocation) {

  function getPosition(position) {

    $.post("{{path('login_log_location')}}",  {  
     latitude:position.coords.latitude,
     longitude:position.coords.longitude })
     .done(function(data)  {
     $("#divputinarea").html('<form method=post action="
{{path('login_log_yourarea')}}"><button class="btn btn-large   
btn-primary">Find your nearest matches</button>');

 });

      }

navigator.geolocation.getCurrentPosition(getPosition, {  
maximumAge: 0,timeout: 100,enableHighAccuracy:true} ); 

  }  

  }

 </script> 

1 Answer 1

2

The getCurrentPosition method takes three parameters: a success callback, an error callback and an object containing configuration parameters.

navigator.geolocation.getCurrentPosition(
    successCallback,
    errorCallback,
    {frequency:5000, maximumAge: 0, timeout: 100, enableHighAccuracy:true} 
); 

Here's a jsfiddle.

EDIT: You can modify your code to define these three parameters.

function getPosition(position) {
    $.post("{{path('login_log_location')}}",  {  
     latitude:position.coords.latitude,
     longitude:position.coords.longitude })
     .done(function(data)  {
         $("#divputinarea").html('<form method=post action="
    {{path('login_log_yourarea')}}"><button class="btn btn-large   
    btn-primary">Find your nearest matches</button>');
     });
  }

function onError(positionError) {
    // error handling goes here
}

function getCoordPosition() { 
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(   
        getPosition, // success callback
        onError, // error callback
        {maximumAge: 0,timeout: 100,enableHighAccuracy:true} // extra params
    ); 
  }  
}
1
  • @Nanchi I'm sure your jsfiddle works, but I still can't find a way of making with my jquery code see above attempt
    – GAV
    Nov 3, 2014 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.