H5 is used to obtain the instance code of the user's geographic location. h5 instances

Source: Internet
Author: User

H5 is used to obtain the instance code of the user's geographic location. h5 instances

In a recent project, a function is provided to obtain the surrounding merchants, which requires the use of the geographic location system,

So I thought of using h5's geographic location. After reading the following documents, I simply wrapped the code.

Const getPosition = (timeout = 10000, maximumAge = 60000, enableHighAcuracy = false) => new Promise (resolve, reject) => {if (! Navigator &&! Navigator. geolocation) {return reject (new Error ('geolocation api not Ted orted')} const success = (loc) =>{ const location = {latitude: loc. coords. latitude, // latitude longpolling: loc. coords. longpolling, // longitude accuracy: loc. coords. accuracy // precision} resolve (location)} const error = () => reject ('error error') navigator. geolocation. getCurrentPosition (success, error, {enableHighAcuracy, // indicates that the browser obtains the location with high precision. The default value is fal. Se timeout, // specify the time-out time for obtaining the geographic location. The default time is not limited. The unit is millisecond maximumAge // The maximum validity period. When you obtain the geographic location again, this parameter specifies how long it will take to obtain the location again. }) // Use the example getPosition (). then (pos => pos). catch (err => console. log (err ))

H5's api for obtaining geographical locations is indeed very useful. However, given that Obtaining users' geographical locations belongs to the privacy of users, authorization is required.

Regardless of whether the user refuses or permits authorization, the site will be cached by the browser, and the user will not be asked again during the next visit, unless the user manually removes this provision. Remove the rule address from chrome in the settings, such:

In addition, if the user rejects the request, you can select an ip address to obtain the longitude and latitude. Generally, the error is relatively large.

Ipip.net is a website that obtains the geographical location information of users through ip addresses.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.