This side dish just started learning HTML5, and now I am very interested in Geolocation. I combined it with the API of Google Map to realize the basic map positioning function.
1. Get the current geographical location
Call the method void getCurrentPosition(onSuccess, onError, options);.
Among them, onSuccess is the callback function executed when the current location information is successfully obtained, onError is the callback function executed when the current location information fails to be obtained, and options is some optional familiar lists. The second and third parameters are optional attributes.
In the onSuccess callback function, the parameter position is used, which represents a specific position object and represents the current position. It has the following attributes:
•latitude: the latitude of the current geographical location.
•longitude: The longitude of the current geographical location.
•altitude: the altitude of the current location (null if it cannot be obtained).
•accuracy: The accuracy of the obtained latitude and longitude (in meters).
•altitudeAccurancy: The longitude of the obtained altitude (in meters).
•heading: The forward direction of the device. Represented by the clockwise rotation angle facing the direction of the object (null if it cannot be obtained).
•speed: The forward speed of the device (in meters/second, null if it cannot be obtained).
•timestamp: The time when the geographical location information was obtained.
In the onError callback function, the error parameter is used. It has the following attributes:
•code: error code, with the following values.
1. The user rejected the location service (attribute value is 1);
2. Unable to obtain location information (attribute value is 2);
3. Timeout error in obtaining information (attribute value is 3).
•message: String containing specific error information.
In the options parameter, the optional attributes are as follows:
•enableHighAccuracy: Whether to require high-precision geographical location information.
•timeout: Set the timeout (unit: milliseconds).
•maximumAge: Valid time for caching geographical location information (unit: milliseconds).
Be sure to write the following code to determine whether the browser supports HTML5 to obtain geographical location information, so as to be compatible with earlier browsers that do not support it.