Skip to content

Retrieves the GPS coordinates of the Browser with Javascript. Uses an inline callback. Normalizes the output.

Notifications You must be signed in to change notification settings

campsoftware/locationGet-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

locationGet-js

Summary

Retrieves the GPS coordinates of the Browser.

  • Uses an inline callback.
  • Normalizes the output.
  • Makes the async call feel like a sync call, but it's still async.
  • This is a good template for similar async calls.

Calling Example

locationGet( function ( coords ) { alert( coords['ErrorCode'] + '|' + coords['Latitude'] ); } );

Function

function locationGet( callbackFunction ) {
    // Calling Example
    // locationGet( function ( coords ) { alert( coords['ErrorCode'] + '|' + coords['Latitude'] ); } );
    if ( navigator.geolocation ) {
        navigator.geolocation.getCurrentPosition(
            function ( position ) {
                let result = {};
                result[ 'ErrorCode' ] = "0";
                result[ 'ErrorDesc' ] = "";
                result[ 'Latitude' ] = position.coords.latitude;
                result[ 'Longitude' ] = position.coords.longitude;
                result[ 'Altitude' ] = position.coords.altitude;
                result[ 'Heading' ] = position.coords.heading;
                result[ 'Speed' ] = position.coords.speed;
                result[ 'Accuracy' ] = position.coords.accuracy;
                result[ 'AccuracyAltitude' ] = position.coords.altitudeAccuracy;
                callbackFunction( result );
            },
            function ( err ) {
                let result = {};
                result[ 'ErrorCode' ] = err.code;
                result[ 'ErrorDesc' ] = err.message;
                callbackFunction( result );
            },
            {
                enableHighAccuracy: true,
                timeout: 15000,
                maximumAge: 0
            }
        )
    } else {
        let result = {};
        result[ 'ErrorCode' ] = -1;
        result[ 'ErrorDesc' ] = "Browser does not support Geolocation";
        callbackFunction( result );
    }
}

About

Retrieves the GPS coordinates of the Browser with Javascript. Uses an inline callback. Normalizes the output.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published