Home > Web Front-end > H5 Tutorial > body text

Example tutorial on parsing HTML5 geolocation

零下一度
Release: 2017-05-26 16:02:58
Original
1840 people have browsed it

Test demo's github address: github.com/lily1010/html5_geolocation

HTML5 Geolocation API is used to obtain the user's geographical location. Given that this feature may violate user privacy, user location information is not available unless the user consents.

1 Browser-based HTML5 search for geographical location

The GPS positioning function in HTML5 is encapsulated in the navigator.geolocation attribute. There are three methods:

(1) getCurrentPosition only gets the user's position once

(2) watchPosition returns the user's current position, and continues to return the updated position when the user moves (just like the GPS on a car).

(3) clearWatch() - Stop watchPosition() method

The format of the two getCurrentPosition and watchPosition methods is

getCurrentPosition(successCallback,errorCallback, positionOptions) and watchPosition(successCallback, errorCallback, positionOptions)

(1)successCallback represents the callback function after the function is successfully called. This function has one parameter, object literal format, indicating acquisition User location data.

HTML5 geolocation0

(2)errorCallback indicates the error code returned. It contains the following two attributes:

1、message:错误信息
2、 code:错误代码。

其中code错误代码包括以下四个值:
1 位置服务被拒绝
2 暂时获取不到位置信息
3 获取信息超时
4 未知错误
Copy after login

(3) positionOptions data format is JSON, with three optional attributes:

1、enableHighAcuracy — 布尔值: 表示是否启用高精确度模式,如果启用这种模式,浏览器在获取位置信息时可能需要耗费更多的时间。
2、timeout — 整数: 表示浏览需要在指定的时间内获取位置信息,否则触发errorCallback。
3、maximumAge — 整数/常量: 表示浏览器重新获取位置信息的时间间隔。
Copy after login

Let’s take a look at the test example: (Note that positioning must be turned on before you can see the effect)

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8"/>  
    <title>基于浏览器的HTML5查找地理位置</title> 
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> 
  <script>
  var options={
       enableHighAccuracy:true,   //高精度定位参数
       maximumAge:1000
   }
   function getLocation(){
       if(navigator.geolocation){
           //浏览器支持geolocation
           navigator.geolocation.getCurrentPosition(onSuccess,onError,options);  //getCurrentPosition 只获取一次用户的地理位置函数
           //onSuccess成功返回的回调函数(必选),onError失败返回的回调函数(可选),设置精确度等参数(可选options)
           
           //navigator.geolocation.watchPosition(onSuccess,onError,options);  //watchPosition 继续获取用户的位置,适合于导航
           //onSuccess成功返回的回调函数(必选),onError失败返回的回调函数(可选),设置精确度等参数(可选options)
       }else{
           //浏览器不支持geolocation
           alert (&#39;您的浏览器暂不支持定位&#39;);
       }
   }
   //成功时
   function onSuccess(position){
     //返回用户位置
     //经度
     var longitude =position.coords.longitude;
     
     //纬度
     var latitude = position.coords.latitude;
     
     //精确度
     var accuracy = position.coords.accuracy;
     
     //高度精确度
     var altitudeAccuracy = position.coords.altitudeAccuracy;
     
     //设备正北顺时针前进的方位
     var heading = position.coords.heading;
     
     //设备外部环境的移动速度(m/s)
     var speed = position.coords.speed;
     
     //当位置捕获到时的时间戳
     var timestamp = position.timestamp;
     
     document.getElementById("container").innerHTML= "您的经度是="+longitude+&#39;<br>&#39;
     +"您的纬度是="+latitude+&#39;<br>&#39;+"您的精确度是="+accuracy+&#39;<br>&#39;
     +"您的高度精确度是="+altitudeAccuracy+&#39;<br>&#39;+"您的设备正北顺时针前进的方位是="+heading+&#39;<br>&#39;
     +"您的设备外部环境的移动速度(m/s)是="+speed+&#39;<br>&#39;+"您的当位置捕获到时的时间戳是="+timestamp+&#39;<br>&#39;;
   }
   //失败时
   function onError(error){
     switch(error.code){
       case 1:alert("位置服务被拒绝");break;
       case 2:alert("暂时获取不到位置信息");break;
       case 3:alert("获取信息超时");break;
       case 4:alert("未知错误");break;
     }
   }
   window.onload=getLocation;
   </script>
</head>
<body>
   <p id="container" style="300px;height: 300px"></p>
</body>
</html>
Copy after login

The above code is best tested on a mobile phone, because Google Chrome is blocked in China, positioning, you know

二 HTML5 geolocation calls Baidu map api

Baidu map manual address: developer.baidu.com/map/jsdemo-mobile.htm#i7_1

Let me explain in advance that HTML5 is not precise positioning, so on the map There is always an error of several hundred meters when checking the effect

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8"/>  
    <title>基于浏览器的HTML5查找地理位置和调取百度地图api</title> 
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <!-- 百度API -->
         <script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script>  
  <script>
   var options={
       enableHighAccuracy:true,   //高精度定位参数
       maximumAge:1000
   }
   function getLocation(){
       if(navigator.geolocation){
           //浏览器支持geolocation
           navigator.geolocation.getCurrentPosition(onSuccess,onError,options);  //getCurrentPosition 只获取一次用户的地理位置函数
           //onSuccess成功返回的回调函数(必选),onError失败返回的回调函数(可选),设置精确度等参数(可选options)
           
           //navigator.geolocation.watchPosition(onSuccess,onError,options);  //watchPosition 继续获取用户的位置,适合于导航
           //onSuccess成功返回的回调函数(必选),onError失败返回的回调函数(可选),设置精确度等参数(可选options)
       }else{
           //浏览器不支持geolocation
           alert (&#39;您的浏览器暂不支持定位&#39;);
       }
   }
   //成功时
   function onSuccess(position){
     //返回用户位置
     //经度
     var longitude =position.coords.longitude;
     //纬度
     var latitude = position.coords.latitude;
     //使用百度地图API
     //创建地图实例  
     var map =new BMap.Map("container");
     //创建一个坐标
     var point =new BMap.Point(longitude,latitude);
     //地图初始化,设置中心点坐标和地图级别  
     map.centerAndZoom(point,15);
     map.addOverlay(new BMap.Marker(point)); //在地图上你的位置显示红色点点
   }
   //失败时
   function onError(error){
     switch(error.code){
       case 1:alert("位置服务被拒绝");break;
       case 2:alert("暂时获取不到位置信息");break;
       case 3:alert("获取信息超时");break;
       case 4:alert("未知错误");break;
     }
   }
   window.onload=getLocation;
   </script>
</head>
<body>
   <p id="container" style="300px;height: 300px"></p>
</body>
</html>
Copy after login

The above code has been tested on the mobile phone. The error is a bit large, but it is still not suitable for precise positioning. Positioning in the city is still good

[ Related recommendations】

1. Free h5 online video tutorial

2. HTML5-Geolocation APIs sample code

3 . html5 navigator.geolocation is a case of obtaining geographical location code based on the browser

4. html5 Guide (4) - Detailed explanation of using Geolocation

5 . Detailed explanation of the sample code sharing of Html5 Geolocation to obtain geographical location information

6. Detailed explanation of how to use the Geolocation API of HTML5

7. HTML5 geographical positioning Geolocation-API and Haversine geospatial distance algorithm (graphics and text)

The above is the detailed content of Example tutorial on parsing HTML5 geolocation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!