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.