5
\$\begingroup\$

I've written this code that I've been using for a while now to check for browser geolocation services for Google Maps applications. I have to say that I am not very happy with it as I've had to hack it quite a bit to work in IE9.

Could I please get some critical assessment of the following and possible suggestions for an elegant solution? I need to stress that I do not want to use Modernizr or Gears. However, if there are good arguments, I'm willing to listen.

Notes: All the vars and functions in this script do pretty much as the function or var describes. All variables are declared as jQuery objects. start() launches Google Maps once the latitudes/longitudes are set.

function checkGeolocate()
{
    return 'geolocation' in navigator;
}

if (checkGeolocate()) 
{
    // Note that the if condition does not start until the end of the
    // entire geolocation procedure.

    if(navigator.geolocation.getCurrentPosition(function(position) 
    {
        userLat.val(position.coords.latitude);
        userLng.val(position.coords.longitude);
        userDist.val(25);

        start();

        mapControls.fadeIn(200);
        mapOverlay.fadeOut(100);
    }, 
    function(error) 
    {
        console.log(error);

        displayPostcodeSelect();
    },
    {
        timeout : 5000,
        maximumAge : 90000,
        enableHighAccuracy : true
    }))
    {
        // Do nothing as script has started
        // This if clause is to force IE9 to error out
        // instead of hanging when geolocation throws a PERMISSION_DENIED error.
    } 
    else {
        displayPostcodeSelect();
    }
} 
else 
{
    console.log('no geo support');

    displayPostcodeSelect();
}
\$\endgroup\$
5
\$\begingroup\$

A tricky question,

navigator.geolocation.getCurrentPosition will always return undefined, regardless of browser or whether or not the the user allowed the location to be retrieved.

That means that //Do nothing as script has started, This if clause is to force IE9 to error out instead of hanging when geolocation throws a PERMISSION_DENIED error. sounds like a superstitious approach. I believe you that it works, but it's not because you provided an empty if.

Also please read this, if you must provide callbacks, then that means that //Note that the if condition does not start until the end of the entire geolocation procedure. is wrong.

From the little code in your question, I believe you should always call displayPostcodeSelect first, and if there is a geoLocation success you trigger your function. This means you no longer need to catch failures anymore since you want displayPostcodeSelect in that case anyway.

Furthermore, production code should not have console.log, it just looks bad.

Finally, 1 liner function that are used only once do not make sense, you should inline the checkGeolocate function.

All in all, I would counter propose:

//Start by showing the PostcodeSelect
displayPostcodeSelect();
if ('geolocation' in navigator) 
{
  var geoOptions =
  {
        timeout            : 5000,  //5 seconds
        maximumAge         : 90000, //1.5 minutes
        enableHighAccuracy : true
  };  
  navigator.geolocation.getCurrentPosition(function(position) 
  {
        userLat.val(position.coords.latitude);
        userLng.val(position.coords.longitude);
        userDist.val(25);

        start();

        mapControls.fadeIn(200);
        mapOverlay.fadeOut(100);
  }, function(){}, geoOptions );
}
\$\endgroup\$
  • \$\begingroup\$ Apologies I haven't accepted this in such a long time. \$\endgroup\$ – David Barker Apr 23 '15 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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