HTML5 Geolocation
HTML5 Geolocation API is used to obtain the user's geographical location.
Note: In view of the fact that this feature may violate the user's privacy, the user's location information is not available unless the user agrees. The browser will pop up a reminder when using this feature. frame.
## 1. Several methods of geographical positioning
IP address, GPS, Wifi, GSM/CDMA2. Geographic location acquisition process
1. The user opens the web page where the geolocation needs to be obtained application.
3. Browser support
4. Methods of geographical positioning in HTML5
GeolocationAPI exists in the navigator object and only contains 3 methods:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">点击按钮获取您当前坐标(可能需要比较长的时间获取):</p> <button onclick="getLocation()">点我</button> <script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else{x.innerHTML="该浏览器不支持获取地理位置。";} } function showPosition(position) { x.innerHTML="纬度: " + position.coords.latitude + "<br>经度: " + position.coords.longitude; } </script> </body> </html>Instance analysis :Detect whether geolocation is supportedIf supported, run the getCurrentPosition() method. If not supported, a message is displayed to the user. If getCurrentPosition() runs successfully, a coordinates object is returned to the function specified in the parameter showPositionThe showPosition() function obtains and displays the longitude and latitudeHandling errors and RejectionThe second parameter of the getCurrentPosition() method is used to handle errors. It specifies the function to be run when obtaining the user's location fails:
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denies access to geographical location request. "
" " x .innerHTML="The request for the user's geographical location timed out."
##getCurrentPosition() method - return data
If the position is obtained successfully, the getCurrentPosition() method returns object. The latitude, longitude, and accuracy properties are always returned. If available, the other following properties are returned.
Properties #coords.longitude Longitude as a decimal number coords.accuracy Position accuracy
coords.altitudeAccuracy Position of Altitude accuracy
coords.heading Direction, in degrees from true north
coords.speed Speed, in meters per second timestamp Date/time of response
You can also get the geographical location (only supported by firefox)