<!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