Home > Web Front-end > H5 Tutorial > body text

Sample code for HTML5-Geolocation APIs

黄舟
Release: 2017-04-01 11:42:45
Original
1640 people have browsed it

1.navigator.geolocation
HTML5The main object related to geographical location is navigator.geolocation, which has some methods and properties, Detecting browser support for HTML5 geolocation is also detecting this object.

2.getCurrentPosition, watchPosition and clearWatch methods
These are the two core methods of geolocation. The first method only obtains the geographical location information, and the latter method obtains the geographical location information at a certain time interval. Their parameters are the same, but the return values ​​are different. The latter returns a watchId. Passing the watchId as a parameter to the clearWatch method can terminate the geographical location information. Request for location information.
getCurrentPosition is in the shape of:

void getCurrentPosition(in PositionC
all
back successCallback, 
                           in optional PositionErrorCallback errorCallback, 
                           in optional PositionOptions options);
Copy after login

The first parameter is a function , which is used to process the successfully received geographical location information. It usually receives a position object as a parameter , and provide the received geolocation information.
The second parameter is optional and is also a function for error handling. This function usually receives an error object as a parameter, and error contains error information.
The third parameter is used to further control the geographical location information and is also optional. It is usually enclosed in curly braces, and it usually has three values: ​
​ enableHighAccuracy: used to control accuracy, its effect may have side effects.
timeout: Used to specify the timeout for geographical location information requests, in milliseconds.
MaximumAge: used to specify the update frequency of geographical location information, in milliseconds.

An example is as follows:

navigator.geolocation.getCurrentPosition(up
date
Location,han
dl
eLocationError, 
                                         {timeout:10000});
Copy after login

3.psition object
Its definition is as follows:

interface Position {
      readonly attribute Co
ord
inates coords;
      readonly attribute DOMTimeStamp timestamp;
   };
Copy after login

It has a sub-object coords and a property timestamp.
coords: is a Coordinates object, which is defined as follows:

   interface Coordinates {
         readonly attribute double latitude; //维度
         readonly attribute double lon
git
ude; //经度
         readonly attribute double? altitude; //高程(/m)
         readonly attribute double accuracy; //经度和维度的精确度(/m)
         readonly attribute double? altitudeAccuracy; //高程精确度(/m)
         readonly attribute double? 
head
ing; //移动方向(/deg)
         readonly attribute double? speed; //移动速度(/m/s)
      };
Copy after login

The properties with question marks will not be implemented in many browsers and devices. If there are no these properties, programming through The value obtained will be null.
The three attributes of latitude, longitude and accuracy are used to provide the requested dimension, longitude and accuracy information respectively. Latitude and longitude are expressed in decimals, and the precision unit is meters.

timestamp: Timestamp.

4.error object
The error object has a code attribute, which is used to specify the error type. You can think of code as an enumeration type. It has four values: 0 (UNKNOWN_ERROR1, 1 (PERMISSION_DENIED), 2 (POSITION_UNAVAILABLE) and
3 (TIMEOUT)
In addition, error also has a message attribute to provide error details

.

The above is the detailed content of Sample code for HTML5-Geolocation APIs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!