search
HomeWeb Front-endH5 TutorialExample tutorial on parsing HTML5 geolocation

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 未知错误

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

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

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>

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>

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!

Statement
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
html5的div一行可以放两个吗html5的div一行可以放两个吗Apr 25, 2022 pm 05:32 PM

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别是什么html5中列表和表格的区别是什么Apr 28, 2022 pm 01:58 PM

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

html5怎么让头和尾固定不动html5怎么让头和尾固定不动Apr 25, 2022 pm 02:30 PM

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有哪些html5中不支持的标签有哪些Mar 17, 2022 pm 05:43 PM

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了哪个列表标签html5废弃了哪个列表标签Jun 01, 2022 pm 06:32 PM

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是什么意思html5是什么意思Apr 26, 2021 pm 03:02 PM

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

Html5怎么取消td边框Html5怎么取消td边框May 18, 2022 pm 06:57 PM

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools