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.
(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 ('您的浏览器暂不支持定位'); } } //成功时 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+'<br>' +"您的纬度是="+latitude+'<br>'+"您的精确度是="+accuracy+'<br>' +"您的高度精确度是="+altitudeAccuracy+'<br>'+"您的设备正北顺时针前进的方位是="+heading+'<br>' +"您的设备外部环境的移动速度(m/s)是="+speed+'<br>'+"您的当位置捕获到时的时间戳是="+timestamp+'<br>'; } //失败时 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 ('您的浏览器暂不支持定位'); } } //成功时 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
6. Detailed explanation of how to use the Geolocation API of HTML5
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!

MicrodatainHTML5enhancesSEOanduserexperiencebyprovidingstructureddatatosearchengines.1)Useitemscope,itemtype,anditempropattributestomarkupcontentlikeproductsorevents.2)TestmicrodatawithtoolslikeGoogle'sStructuredDataTestingTool.3)ConsiderusingJSON-LD

HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
