Get the App
SLTechnology News&Howtos  ›  Development  › 

Methods of handling errors and rejections in HTML5 geolocation

Shulou Source: shulou.com Published: 2022-06-01 05:40:24 09月23日 Update

In this article, the editor introduces in detail "HTML5 geolocation handling errors and rejection methods". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "HTML5 geolocation handling errors and rejection methods" can help you solve your doubts.

Handle errors and rejections

The second parameter of the getCurrentPosition () method is used to handle errors. It specifies the function to run when it fails to get the user's location:

Example

Function showError (error)

{

Switch (error.code)

{

Case error.PERMISSION_DENIED:

X [XSS _ clean] = "the user rejects the request for geolocation."

Break

Case error.POSITION_UNAVAILABLE:

X [XSS _ clean] = "location information is not available."

Break

Case error.TIMEOUT:

X [XSS _ clean] = "request user geolocation timed out."

Break

Case error.UNKNOWN_ERROR:

X [XSS _ clean] = "unknown error."

Break

}

}

Error code:

Geolocation is not allowed for Permissiondenied- users

Positionunavailable- cannot get the current location

Timeout- operation timed out

Display the results in the map

To display the results in a map, you need to access mapping services that use latitude and longitude, such as Google Maps or Baidu Maps:

Example

Function showPosition (position)

{

Var latlon=position.coords.latitude+ "," + position.coords.longitude

Var img_url= "http://maps.googleapis.com/maps/api/staticmap?center="

+ latlon+ "& zoom=14&size=400x300&sensor=false"

Document.getElementById ("mapholder") [xss_clean] = "

"

}

After reading this, the article "HTML5 geolocation handling errors and rejection methods" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Error geography processing location map method location user article content geographic location example result appropriateness code information function parameter idea new knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Apple Shulou Technology OPPO Reno