亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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/CDMA

2. Geographic location acquisition process

1. The user opens the web page where the geolocation needs to be obtained application.

2. The application requests the geographical location from the browser, and the browser pops up a query asking the user whether to share the geographical location.

3. Assuming the user allows it, the browser queries the relevant information from the device.

4. The browser sends relevant information to a trusted location server, and the server returns the specific geographical location.

3. Browser support

IE9.0+, FF3.5+, Safari5.0+, Chrome5.0+, Opera10.6 + Support geolocation.

Note: For devices with GPS, such as iPhone (IPhone3.0+, Android2.0+), geographical positioning is more accurate.

4. Methods of geographical positioning in HTML5

GeolocationAPI exists in the navigator object and only contains 3 methods:

1. getCurrentPosition //Current position

2. watchPosition //Monitoring position

3.clearWatch //Clear monitoring

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>
<p id="demo">點擊按鈕獲取您當(dāng)前坐標(biāo)(可能需要比較長的時間獲?。?lt;/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>經(jīng)度: " + position.coords.longitude;
  }
</script>
</body>
</html>

Instance analysis :

Detect whether geolocation is supported

If 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 showPosition

The showPosition() function obtains and displays the longitude and latitude

Handling errors and Rejection

The 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.altitude Altitude, in meters above sea level

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)


p.address.country country

p.address.region Province

p.address.city City

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>基于瀏覽器的HTML5查找地理位置</title> <!-- 百度API --> <script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script> <script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(navigator.geolocation){ //瀏覽器支持geolocation navigator.geolocation.getCurrentPosition(onSuccess,onError,options); }else{ //瀏覽器不支持geolocation } } //成功時 function onSuccess(position){ //返回用戶位置 //經(jīng)度 var longitude =position.coords.longitude; //緯度 var latitude = position.coords.latitude; //使用百度地圖API //創(chuàng)建地圖實例 var map =new BMap.Map("container"); //創(chuàng)建一個坐標(biāo) var point =new BMap.Point(longitude,latitude); //地圖初始化,設(shè)置中心點坐標(biāo)和地圖級別 map.centerAndZoom(point,15); } //失敗時 function onError(error){ switch(error.code){ case 1: alert("位置服務(wù)被拒絕"); break; case 2: alert("暫時獲取不到位置信息"); break; case 3: alert("獲取信息超時"); break; case 4: alert("未知錯誤"); break; } } window.onload=getLocation; </script> </head> <body> <!-- 結(jié)果會顯示位置服務(wù)被拒絕 --> <div id="container" style="width:600px;height:600px"></div> </body> </html>
submitReset Code