Home Web Front-end H5 Tutorial Summary of development and application of geographical location positioning api interface in html5_html5 tutorial skills

Summary of development and application of geographical location positioning api interface in html5_html5 tutorial skills

May 16, 2016 pm 03:50 PM
api geographical location

Several methods of geolocation positioning: IP address, GPS, Wifi, GSM/CDMA

Geolocation acquisition process :
1. The user opens the web application that needs to obtain the geolocation.
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.

Implementation of HTML5 geographical location:
1. Implement browser-based (without back-end support) technology to obtain the user’s geographical location
2. Accurately locate the user’s geographical location (The accuracy is up to 10m, depending on the device)
3. Continuously track the user’s geographical location
4. Interact with Google Map or Baidu Map to present location information

Geolocation API is used to connect users The current geographical location information is shared with trusted sites, which involves user privacy and security issues. Therefore, when a site needs to obtain the user's current geographical location, the browser will prompt the user to "allow" or "deny".
First check which browsers support Geolocation API:
IE9.0, FF3.5, Safari5.0, Chrome5.0, Opera10.6, IPhone3.0, Android2.0
Geolocation API exists in The navigator object only contains 3 methods:

Copy code
The code is as follows:

1. getCurrentPosition //Current position
2. watchPosition //Monitoring position
3. clearWatch //Clear monitoring
navigator.geolocation.getCurrentPosition( …, function(error){
switch(error .code){
case error.TIMEOUT :
alert( " Connection timed out, please try again" );
break;
case error.PERMISSION_DENIED :
alert( " You have refused to use Location sharing service, query has been canceled" );
break;
case error.POSITION_UNAVAILABLE :
alert( ", Sorry, location services are temporarily unavailable for your planet" );
break;
}
});

watchPosition is like a tracker paired with clearWatch.
watchPosition and clearWatch work a bit like setInterval and clearInterval.
var watchPositionId = navigator.geolocation.watchPosition(success_callback, error_callback, options);
navigator.geolocation.clearWatch(watchPositionId );

HTML 5 provides a series of APIs such as geographical location to provide users with It is convenient for users to create LBS geographical applications. First, in browsers that support HTML 5, when the API is turned on, the user will be asked whether they agree to use the API. Otherwise, it will not be turned on to ensure safety.
1. Turn it on to determine whether the browser supports LBS api

Copy code
Code As follows:

function isGeolocationAPIAvailable()
{
var location = "No, Geolocation is not supported by this browser.";
if (window.navigator.geolocation) {
location = "Yes, Geolocation is supported by this browser.";
}
alert(location);
}

In the above example, there is still displayError In the method, the exception is caught;
2. Get the user’s geographical location
Just use getCurrentPosition;

Copy code
The code is as follows:

function requestPosition() {
if (nav == null) {
nav = window.navigator;
}
if (nav != null) {
var geoloc = nav.geolocation;
if (geoloc != null) {
geoloc.getCurrentPosition(successCallback);
}
else {
alert("Geolocation API is not supported in your browser");
}
}
else {
alert("Navigator is not found");
}
}

When the geographical location is successfully obtained, a callback method will be generated to process the returned result,

Copy code
The code is as follows:

function setLocation(val, e) {
document.getElementById(e).value = val;
}
function successCallback(position)
{
setLocation(position.coords.latitude, "latitude"); setLocation(position.coords.longitude, "longitude");
}

3. A very common question is how to track the user’s changing geographical location. Here is a summary of the two APIs used
1 watchPosition
The example is as follows:

Copy code
The code is as follows:

function listenForPositionUpdates() {
if (nav == null) {
nav = window.navigator;
}
if (nav != null ) {
var geoloc = nav.geolocation;
if (geoloc != null) {
watchID = geoloc.watchPosition(successCallback);
} else {
alert("Geolocation API is not supported in your browser");
}
} else {
alert("Navigator is not found");
}
}

Then in In the successCallback, you can set the latest geographical location to be displayed:

Copy the code
The code is as follows:

function successCallback(position){
setText(position.coords.latitude, "latitude"); setText(position.coords.longitude, "longitude");
}

If you do not want real-time tracking, you can cancel it:
function clearWatch(watchID) {
window.navigator.geolocation.clearWatch(watchID);
}
4. How to handle exceptions
When you encounter an exception, you can catch it:

Copy the code
The code is as follows:

if (geoloc != null) {
geoloc.getCurrentPosition(successCallback, errorCallback);
}
function errorCallback(error) {
var message = "";
switch (error.code) {
case error.PERMISSION_DENIED:
message = "This website does not have permission to use "
"the Geolocation API";
break;
case error.POSITION_UNAVAILABLE:
message = "The current position could not be determined.";
break;
case error.PERMISSION_DENIED_TIMEOUT:
message = "The current position could not be determined "
"within the specified timeout period.";
break;
}
if (message == "") {
var strErrorCode = error.code.toString();
message = " The position could not be determined due to "
"an unknown error (Code: " strErrorCode ").";
}
alert(message);
}

5. Display the location on google map (provided that google map api and other settings are set up)

Copy code
The code is as follows:

function getCurrentLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showMyPosition,showError );
}
else
{
alert("No, Geolocation API is not supported by this browser.");
}
}
function showMyPosition(position)
{
var coordinates=position.coords.latitude "," position.coords.longitude;
var map_url="http://maps.googleapis.com/maps/api/staticmap?center="
coordinates "&zoom=14&size=300x300&sensor=false";
document.getElementById("googlemap").innerHTML="";
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
alert("This website does not have permission to use the Geolocation API")
break;
case error.POSITION_UNAVAILABLE:
alert("The current position could not be determined.")
break;
case error.TIMEOUT:
alert("The current position could not be determined.") position could not be determined within the specified time out period.")
break;
case error.UNKNOWN_ERROR:
alert("The position could not be determined due to an unknown error.")
break;
}
}
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
How to modify Douyin IP location How to modify Douyin IP location May 04, 2024 pm 04:36 PM

Yes, you can modify the Douyin IP location as follows: Open Douyin and edit your profile. Modify the city information and select the city or region you want to display. Log out and log back in for the changes to take effect.

Does Amap require mobile phone registration? Does Amap require mobile phone registration? May 05, 2024 pm 05:12 PM

Yes, for security, personalized services and account management, Amap requires registration with a mobile phone number. The registration steps include: Open the Amap app, click "My" and "Login/Register", select a mobile phone number to register, enter the mobile phone number to get the verification code, set a password to complete the registration.

How to post pictures and videos on Weibo How to post pictures and videos on Weibo May 03, 2024 am 01:15 AM

The steps for posting pictures and videos at the same time on Weibo are as follows: Select related or complementary pictures and videos. Open the Weibo client and click the Publish button. Select the "Pictures & Videos" tab. Add pictures and videos (up to 9 pictures and 1 video). Enter text and add relevant information. Just publish it.

How to reset Douyin recommendations? How to change recommendation to featured? How to reset Douyin recommendations? How to change recommendation to featured? May 08, 2024 pm 03:52 PM

As a social platform focusing on short videos, Douyin’s recommendation algorithm is one of its core functions. It can recommend relevant video content based on users' interests and behaviors. Sometimes users may want to reset the recommendation algorithm to get content more in line with their preferences. So, how to reset Douyin recommendations? How to change Douyin recommendation to featured? This article will answer both questions for you. 1. How to reset Douyin recommendations? 1. Open Douyin APP and enter your personal homepage. 2. Click the "Settings" icon in the upper right corner to enter the settings page. 3. On the settings page, find the "Recommended Management" option and click to enter. 4. On the recommendation management page, you can see your interest tags and interest preferences. You can select or deselect different

How is Douyin's IP address displayed? Does the IP address show real-time location? How is Douyin's IP address displayed? Does the IP address show real-time location? May 02, 2024 pm 01:34 PM

Users can not only watch a variety of interesting short videos on Douyin, but also publish their own works and interact with netizens across the country and even the world. In the process, Douyin’s IP address display function has attracted widespread attention. 1. How is Douyin’s IP address displayed? Douyin’s IP address display function is mainly implemented through geographical location services. When a user posts or watches a video on Douyin, Douyin automatically obtains the user's geographical location information. This process is mainly divided into the following steps: first, the user enables the Douyin application and allows the application to access its geographical location information; secondly, Douyin uses location services to obtain the user's geographical location information; finally, Douyin transfers the user's geographical location information Geographic location information is associated with the video data they posted or watched and will

How to modify location permissions on TikTok How to modify location permissions on TikTok May 03, 2024 pm 11:24 PM

Steps to modify Douyin location permissions: 1. Open the Douyin app and click "Me". 2. Click the "three horizontal bars icon" in the upper right corner. 3. Select Settings. 4. Find "Privacy Settings" and click on it. 5. Click "Location Services". 6. Select Allow targeting or Only ask when using, as appropriate. 7. After modification, you need to restart the Douyin application to take effect.

How to change recommendation settings on TikTok How to change recommendation settings on TikTok May 04, 2024 am 12:06 AM

Douyin recommendations can be changed by changing the "Content Preferences" settings, including adjusting recommended video types, following interested creators, blocking disliked content, setting video language, geographical location restrictions, following hot topics and clearing search/browsing history .

How to change TikTok time zone settings How to change TikTok time zone settings May 04, 2024 am 01:57 AM

Douyin time zone settings cannot be changed, the time zone will be automatically set based on the current geographical location.

See all articles