JS new API Standard geo-location (navigator.geolocation)

Source: Internet
Author: User

In the new API standard, you can get the current location of the device through Navigator.geolocation, and return a location object where the user can get some information about the latitude and longitude of the object.

Three ways to navigator.geolocation :

1. GetCurrentPosition ()//Current position

2. Watchposition ()//monitor Location

3. Clearwatch ()//clear monitoring

1.getCurrentPosition ()

method of Use:navigator.geolocation.getCurrentPosition (Successcallback, [Errorcallback], [positionoptions]);

A) Successcallback Gets the callback function executed when the location succeeds Eg:function (position) {alert ("Latitude:" +position.coords.latitude+ "; Longitude:" + Position.coords.longitude)};

Successcallback returns a geographic data object position as a parameter that has properties timestamp and coords. timestamp represents the creation time (timestamp) of the geographic data; coords includes seven additional attributes:

1. Coords.latitude: Estimated Latitude
2. Coords.longitude: Estimated Longitude
3. Coords.altitude: Estimated Height
4. Coords.accuracy: Accuracy of the estimated longitude and latitude provided in meters
5. Coords.altitudeaccuracy: The accuracy of the provided height estimate in meters
6. Coords.heading: The angle direction of the current movement of the host device, calculated clockwise relative to the north direction
7. Coords.speed: The current ground speed of the unit in meters per second

Ps:firefox also has the Address property, you can get the detailed addresses, but I get the address is wrong, using the method: Position.address.city, as follows:

Queryinterface:function QueryInterface () {[native code]} STREETNUMBER:200 Street: Renmin Avenue Premises:nullcity: Shanghai county:nullregion: Shanghai Country: China CountryCode:CNpostalCode:nullgetInterface S:function getinterfaces () {[native code]}gethelperforlanguage:function gethelperforlanguage () {[native code]} Contractid:nullclassdescription:nullclassid:nullimplementationlanguage:2flags:8singleton:1threadsafe:2main_ thread_only:4dom_object:8plugin_object:16content_node:64reserved:2147483648

  

For example: Navigator.geolocation.getCurrentPosition (function (p) {      var map= "Dimension" +p.coords.latitude+ "longitude" + P.coords.longitude;     Alert (Var);    });

  

B) The callback function eg:function (error) {alert (error.message) executed when the Errorcallback location fails;}

Errorcallback returns an error data object as an argument that has properties:

1.code: Indicates the reason for failure, returns 1 or 2 or 3, specifically

    PERMISSION_DENIED(a value of 1) indicates that there is no permission to use the Geo-location API

    Position_unavailable (a value of 2) indicates that the location of the device cannot be determined, for example, one or more of the locating Grabber reports an internal error that caused the failure of the entire process

    Timeout (a value of 3) indicates a time-out

View more Http://dev.w3.org/geo/api/spec-source.html#permission_denied_error

2.message: Error message content

C) positionoptions is used to set positionoptions to perform more granular positioning, Positionoptions has three properties {Enablehighaccuracy:boolean, Timeout:long, Maximumage:long}.

Enablehighaccuracy "True or False (default)" Returns a more detailed and accurate structure that is false by default is not enabled, and true is enabled, but results in longer response times and increased power consumption, which is more used on mobile devices.

Timeout device location Get operation time-out setting (not including get user permission time), in milliseconds, if the location is not acquired within the set timeout time, then Errorcallback () will be executed to return code (3). If timeout is not set, then timeout defaults to infinity, and if timeout is negative, the default timeout is 0.

Maximumage Sets the location cache time, in milliseconds, which defaults to 0 if the value is not set, or 0 if a negative number is set. When the value is 0 o'clock, a new location object is retrieved when the position is positioned, which is greater than 0 o'clock, that is, the cache location object, starting from the last time the location was acquired, and if the location time is not more than Maximumage, returns the location in the cache if the maximumage is exceeded. To regain a new location.

2.watchPosition ()

Lecaf

No comment on the swelling?

JS new API Standard geo-location (navigator.geolocation)

In the new API standard, you can get the current location of the device through Navigator.geolocation, and return a location object where the user can get some information about the latitude and longitude of the object.

Three ways to navigator.geolocation :

1. GetCurrentPosition ()

2. Watchposition ()

3. Clearwatch ()

GetCurrentPosition ()

Method of Use: Navigator.geolocation.getCurrentPosition (Successcallback, [Errorcallback], [positionoptions]);

A) Successcallback Gets the callback function executed when the location succeeds Eg:function (position) {alert ("Latitude:" +position.coords.latitude+ "; Longitude:" + Position.coords.longitude)};

Successcallback returns a geographic data object position as a parameter that has properties timestamp and coords. timestamp represents the creation time (timestamp) of the geographic data; coords includes seven additional attributes:

1. Coords.latitude: Estimated Latitude
2. Coords.longitude: Estimated Longitude
3. Coords.altitude: Estimated Height
4. Coords.accuracy: Accuracy of the estimated longitude and latitude provided in meters
5. Coords.altitudeaccuracy: The accuracy of the provided height estimate in meters
6. Coords.heading: The angle direction of the current movement of the host device, calculated clockwise relative to the north direction
7. Coords.speed: The current ground speed of the unit in meters per second

Ps:firefox also has the Address property, you can get the detailed addresses, but I get the address is wrong, using the method: Position.address.city, as follows:

Queryinterface:function QueryInterface () {[native code]}
STREETNUMBER:200 No.
Street: Renmin Avenue
Premises:null
City: Shanghai
County:null
Region: Shanghai, China
Country: China
Countrycode:cn
Postalcode:null
Getinterfaces:function getinterfaces () {[native code]}
Gethelperforlanguage:function Gethelperforlanguage () {[native code]}
Contractid:null
Classdescription:null
Classid:null
Implementationlanguage:2
Flags:8
Singleton:1
Threadsafe:2
Main_thread_only:4
Dom_object:8
Plugin_object:16
Content_node:64
reserved:2147483648

B) The callback function eg:function (error) {alert (error.message) executed when the Errorcallback location fails;}

Errorcallback returns an error data object as an argument that has properties:

1.code: Indicates the reason for failure, returns 1 or 2 or 3, specifically

    PERMISSION_DENIED(a value of 1) indicates that there is no permission to use the Geo-location API

    Position_unavailable (a value of 2) indicates that the location of the device cannot be determined, for example, one or more of the locating Grabber reports an internal error that caused the failure of the entire process

    Timeout (a value of 3) indicates a time-out

View more Http://dev.w3.org/geo/api/spec-source.html#permission_denied_error

2.message: Error message content

C) positionoptions is used to set positionoptions to perform more granular positioning, Positionoptions has three properties {Enablehighaccuracy:boolean, Timeout:long, Maximumage:long}.

Enablehighaccuracy "True or False (default)" Returns a more detailed and accurate structure that is false by default is not enabled, and true is enabled, but results in longer response times and increased power consumption, which is more used on mobile devices.

Timeout device location Get operation time-out setting (not including get user permission time), in milliseconds, if the location is not acquired within the set timeout time, then Errorcallback () will be executed to return code (3). If timeout is not set, then timeout defaults to infinity, and if timeout is negative, the default timeout is 0.

Maximumage Sets the location cache time, in milliseconds, which defaults to 0 if the value is not set, or 0 if a negative number is set. When the value is 0 o'clock, a new location object is retrieved when the position is positioned, which is greater than 0 o'clock, that is, the cache location object, starting from the last time the location was acquired, and if the location time is not more than Maximumage, returns the location in the cache if the maximumage is exceeded. To regain a new location.

Watchposition ()

function GetCurrentPosition () similar, watchposition () is the location of the periodic polling device, which also has 3 parameters, the same as GetCurrentPosition ().

Method of Use: Navigator.geolocation.watchPosition (Successcallback, [Errorcallback], [positionoptions]);

To perform the steps:

1. First initialize the attributes within the Positionoptions (detailed above).

2. Determine if there is a cache location object, whether the age of the object is available, whether it exceeds maximumage, and if available and not exceeded, return to the cache location, or re-determine the device location.

3. Position positioning operation:

I. Set up a timer to take the position acquisition operation, if completed before timeout, perform the next step, if not completed before timeout, then execute Errorcallback (), code 3, step out to wait for reactivation.

II. If the location succeeds before timeout, execute Successcallback (), then reset the timer (from the time the acquisition position is successfully recalculated), and continue pending to get the new location. When there is a distinct position with the previous position, the Successcallback () is executed again, and the loop is repeated until the timeout expires or the get operation encounters position_unavailable error Execution Errorcallback () , or perform a clearwatch () operation.

2.clearWatch ()

Used with Watchposition () to stop watchposition () polling.

Watchposition () needs to define a watchid,var Watchid = Watchposition (...) and Clearwatch (Watchid) to stop watchposition (), The use method is similar to setinterval.

Related information:

1. Http://dev.w3.org/geo/api/spec-source.html#geolocation

2. http://kb.cnblogs.com/page/108732/

Reprint Please specify source: http://www.cnblogs.com/lecaf/

If you have any suggestions or questions, please leave a message to discuss.

If you think the article is good, welcome to the lower right corner of the recommendation.

Source: http://www.cnblogs.com/lecaf/

JS new API Standard geo-location (navigator.geolocation)

JS new API Standard geo-location (navigator.geolocation)

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.