Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article

Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article

Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article

Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article

Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article

Thank you for sharing -bjbsair.com/2020-04-07/…

preface

Use the HTML5 Geolocation API to build location-based applications

HTML5 Geolocation support by various browsers

Browser Version Only HTTPS Internet Explorer 9+ -Edge 12+ – Firefox 3.5+ – Chrome 5+ 50+ Safari 5+ 39+ iOS Safari 3.2+ 10.2+ Android Browser 2.1+ 56+ Chrome for Android 57+ 57+ UC Browser for Android 11.4+ –

For security reasons, some browsers allow the Geolocation API to be used only through HTTPS. Using the Geolocation API under HTTP throws an exception. During development, localizations such as 127.0.0.1 and localhost can be used under either protocol.

The Geolocation API is accessed through navigator. Geolocation global object. When accessed for the first time, the user is asked whether the shared location is allowed.

Check whether the browser supports the Geolocation API

// Check whether the browser supports the locationif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }
Copy the code

Example code is as follows:

<! DOCTYPE html> <html lang="en">  
  
<head>  
    <meta charset="UTF-8">  
    <meta >  
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> Get user location <inputtype="button" value="Click to get location" id="btn">  
    <script>  
        let btnBtn = document.getElementById('btn') btnbtn.onclick = () => {// Click the event getAdd()} // successful callbackletSuccess = (position) => {console.log(' obtain position successfully:${position.coords}`); console.log(position.coords); Console. log(position.coords.latitude); / / get latitude coordinates to the console. The log (position. Coords. Longitude); Console. log(position.coords.accuracy); Console. log(position.timestamp); // Get the timestamp of the location} // Failed callbackletError = (positionErr) => {console.log(' Failed to get location:${positionErr.code}+${positionErr.message}`);  
        }  
  
        let options = {  
            enableHightAccuracy: false, // Obtain high precision location information, which may increase response time. Default isfalseTimeout: 30000, // Set the timeout period, in milliseconds, to trigger a failure callback if no information is retrieved. The default value is 0, maximumAge: 0 // Set the maximum time for user location information to be cached.letGetAdd = () = > {the navigator. Geolocation. GetCurrentPosition (success, error, options)} / browser/judgment is no support for positionif(navigator.geolocation){  
            console.log("Available");  
        }else{  
            console.log("Not supported");  
        }  
    </script>  
</body>  
  
</html>  

Copy the code

When the location fails to be retrieved, the failure callback (error function) is called. The returned parameter <positionErr.code identifies the cause of the error >< POSItionErr. message Error message description > PositionErr. code value

  • UNKNOWN_ERROR(0): indicates another error
  • PERMISSION_DENIED(1): The user refused to share location information
  • POSITION_UNAVAILABLE(2): The user location information fails to be obtained
  • TIMEOUT(3): Obtaining user location information has timed out

Recommend the article