"HTML5" geo-positioning

Source: Internet
Author: User

<!DOCTYPE HTML><HTML><Body><PID= "Demo">Click this button to get your coordinates:</P><Buttononclick= "getLocation ()">Try it.</Button><Script>varx=document.getElementById ("Demo");functiongetLocation () {if(navigator.geolocation) {navigator.geolocation.getCurrentPosition (showposition); }  Else{x.innerhtml="Geolocation is isn't supported by this browser.";} }functionshowposition (position) {x.innerhtml="Latitude:" +Position.coords.latitude+   "<br/>longitude:" +Position.coords.longitude; }</Script></Body></HTML>

    • Detect whether geo-targeting is supported
    • If supported, run the GetCurrentPosition () method. If not supported, a message is displayed to the user.
    • If GetCurrentPosition () runs successfully, returns a coordinates object to the function specified in the parameter showposition
    • The Showposition () function obtains and displays the longitude and latitude
<!DOCTYPE HTML><HTML><Body><PID= "Demo">Click this button to get your coordinates:</P><Buttononclick= "getLocation ()">Try it.</Button><Script>varx=document.getElementById ("Demo");functiongetLocation () {if(navigator.geolocation) {navigator.geolocation.getCurrentPosition (showposition,showerror); }  Else{x.innerhtml="Geolocation is isn't supported by this browser.";} }functionshowposition (position) {x.innerhtml="Latitude:" +Position.coords.latitude+   "<br/>longitude:" +Position.coords.longitude; }functionShowError (Error) {Switch(error.code) { Caseerror. Permission_denied:x.innerhtml="User denied the request for geolocation."       Break;  Caseerror. Position_unavailable:x.innerhtml="Location information is unavailable."       Break;  Caseerror. Timeout:x.innerhtml="The request to get user location timed out."       Break;  Caseerror. Unknown_error:x.innerhtml="An unknown error occurred."       Break; }  }</Script></Body></HTML>

    • Permission denied-user does not allow geo-location
    • Position unavailable-Unable to get current position
    • Timeout-operation timed out
GetCurrentPosition () Method-Return Data

Watchposition ()-Returns the user's current location and continues to return to the updated location when the user moves (just like the GPS on the car).

Clearwatch ()-Stop watchposition () method

<!DOCTYPE HTML><HTML><Body><PID= "Demo">Click this button to get your coordinates:</P><Buttononclick= "getLocation ()">Try it.</Button><Script>varx=document.getElementById ("Demo");functiongetLocation () {if(navigator.geolocation) {navigator.geolocation.watchPosition (showposition); }  Else{x.innerhtml="Geolocation is isn't supported by this browser.";} }functionshowposition (position) {x.innerhtml="Latitude:" +Position.coords.latitude+   "<br/>longitude:" +Position.coords.longitude; }</Script></Body></HTML>

"HTML5" geo-positioning

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.