When I started to learn HTML5, I am very interested in Geolocation. Using GoogleMap APIs to implement basic map location functions, I mainly take the following steps: obtaining the current geographic location and calling GoogleMapAPI to obtain the current location information this dish has just begun to learn HTML5. Now it is very interested in the Geolocation, and the basic Map location function is implemented with the Google Map API.
1. Obtain the current geographic location
Call method void getCurrentPosition (onSuccess, onError, options.
OnSuccess is the callback function executed when the current location information is obtained successfully. onError is the callback function executed when the current location information fails to be obtained. options is a list of optional familiarity. The second and third parameters are optional.
In the onSuccess callback function, the position parameter is used to represent a specific position object, indicating the current position. It has the following attributes:
• Latitude: the latitude of the current geographic location.
• Longpolling: the longitude of the current geographic location.
• Altitude: The current altitude (null if not available ).
• Accuracy: the precision of the obtained latitude and longitude (in meters ).
• AltitudeAccurancy: the longitude (in meters) of the obtained altitude ).
• Heading: the forward direction of the device. It is represented by the clockwise rotation angle of the face facing and facing the face (null if it cannot be obtained ).
• Speed: the forward speed of the device (in meters/s and null if the device cannot be obtained ).
• Timestamp: the time when location information is obtained.
The error parameter is used in the onError callback function. It has the following attributes:
• Code: Error code, which has the following values.
1. the user rejects the location service (the attribute value is 1 );
2. The location information cannot be obtained (the property value is 2 );
3. An error occurred while obtaining information (Attribute Value: 3 ).
• Message: a string containing specific error information.
In the options parameter, the optional attributes are as follows:
• EnableHighAccuracy: whether high-precision geographic location information is required.
• Timeout: Set the timeout time (in milliseconds ).
• MaximumAge: the effective time (in milliseconds) for caching geographic location information ).
Note that you should write the following code to determine whether the browser supports HTML5 to obtain geographical location information, so as to be compatible with browsers that are not supported earlier.
The Code is as follows:
If (navigator. geolocation ){
// Obtain the current geographic location information
Navigator. geolocation. getCurrentPosition (onSuccess, onError, options );
} Else {
Alert ("your browser does not support HTML5 to obtain geographic location information. ");
}
2. Call the Google Map API to obtain the current location information.
First, you need to reference the Google Map API script file on the page. The import method is as follows.
The Code is as follows: