The implementation of the Geolocation API in the browser is a navigator.geolocation object, which is commonly used in the following ways.
1. The first method is getcurrentposition()
Invoking this method triggers a dialog box that asks the user to share geolocation information. For example, a dialog box in Firefox:
This method receives 3 parameters: a success callback function, an optional failure callback function, and an optional option object.
The ① success callback function receives a Position object parameter with two properties:coords and timestamp.
The Coords object contains the following location-related information.
- latitude, decimal latitude
- longitude, decimal longitude
- accuracy, accuracy, M
Some browsers also provide information such as altitude/altitude accuracy/speed. The web is commonly used for latitude and longitude.
For example, display the user's location:
Navigator.geolocation.getCurrentPosition (function(position) { $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +Position.coords.longitude);});
The ② failure callback function receives an object that contains two attributes, message and code, which save the error text message, which saves the error code, 1-the user refuses to share, 2-the location is invalid, and 3-times out. In most cases, the error information is saved in the log file. Demonstrate:
Navigator.geolocation.getCurrentPosition (function(position) { $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +position.coords.longitude); }, function (Error) { $ (". Content div.item2"). Text (error.code+ "," +error.message); });
Deny when sharing is denied in Chrome:
will be displayed: Error code 1, information is the user refused to share.
③ The third parameter is the option object, which sets the type of information, there are three options to set:
- Enablehighaccuracy, Boolean value, using the most accurate location information possible.
- Timeout, maximum time to wait for location information, MS
- Maximumage, the last time the coordinate information was obtained, in milliseconds, if the time is up, the coordinate information is obtained again.
Such as:
Navigator.geolocation.getCurrentPosition (function(position) { $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +position.coords.longitude); }, function (Error) { $ (". Content div.item2"). Text (error.code+ "," +error.message); },{ Enablehighaccuracy:false, timeout:+, maximumage: });
It is recommended that the enablehighaccuracy be false, otherwise it will take time to consume power. The Maximumage value can be infinity, which indicates that the last coordinate information is always used.
2. Another method,watchposition(), tracks the user's location. The received parameters are consistent with getcurrentposition ().
The first time the method obtains the position information, it waits for the system to send the signal of position change in place.
You can call the Clearwatch() method to cancel the trace.
The content comes from JavaScript advanced programming.
Geolocation API JavaScript Access user's current location information