Use Geolocation in HTML5 to obtain the geographical location and call GoogleMapAPI to locate on GoogleMap _ html5 tutorial skills-

Source: Internet
Author: User
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:


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.