HTML Geolocation API

The HTML Geolocation API is used to determine the users location.

The HTML Geolocation API is used to determine the user's location.

 

Determine the user's location.

The HTML Geolocation API is used to retrieve information about a user's geographical location.

Because this could affect privacy, location information will not be available unless the user consents.

Note : Geolocation is most accurate on devices with GPS, such as smartphones.

Browser support

The numbers in the table indicate the first browser version that fully supports Geolocation.

API
Geolocation
Google Chrome MS Edge Firefox Safari Opera
5.0 - 49.0 (http)
50.0 (https)
9.0 3.5 5.0 16.0

Note: Starting with Chrome 50, the geolocation API will only work in secure contexts like HTTPS. If your website is hosted on an insecure origin (such as HTTP), requests to retrieve the user's location will no longer work.

 

Use HTML Geolocation

This method getCurrentPosition()is used to return the user's location.

The example below returns the latitude and longitude of the user's location:

For example:

 

The example is explained as follows:

  • Check if geolocation is supported.
  • If supported, run the method getCurrentPosition(). Otherwise, display a message to the user.
  • If the method getCurrentPosition()succeeds, it will return a coordinate object to the function specified in the parameter ( showPosition ).
  • The function showPosition()outputs latitude and longitude.

 

The example above is a very basic Geolocation script, with no error handling.

Error handling and rejection

The second parameter of the method getCurrentPosition()is used for error handling. It specifies a function to run if the user's location information cannot be obtained:

For example:

function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } }

Information about the specific location

This article has shown how to display the user's location on a map.

Geolocation is also very useful for specific location information, such as:

  • Local information updates
  • Display favorites near the user.
  • Turn-by-turn navigation (GPS)

The getCurrentPosition() method returns data.

The method getCurrentPosition()returns an object upon success. The latitude, longitude, and precision attributes are always returned. Other attributes are also returned if available.

 

Thuộc tính Thông tin trả về
coords.latitude Vĩ độ dưới dạng số thập phân (luôn được trả về)
coords.longitude Kinh độ dưới dạng số thập phân (luôn được trả về)
coords.accuracy Độ chính xác của vị trí (luôn được trả về)
coords.altitude Độ cao tính bằng mét trên mực nước biển trung bình (trả về nếu có)
coords.altitudeAccuracy Độ chính xác về độ cao của vị trí (được trả về nếu có)
coords.heading Hướng chuyển động tính bằng độ, theo chiều kim đồng hồ tính từ cực Bắc (trả về nếu có)
coords.speed Tốc độ tính bằng mét trên giây (trả về nếu có)
timestamp Ngày/giờ phản hồi (trả về nếu có)

Geolocation Objects - Other Interesting Methods

Geolocation objects also have other interesting methods:

  • watchPosition()- Returns the user's current location and continues to return the updated location as the user moves (like GPS in a car).
  • clearWatch()- Stop the method watchPosition().

The example below shows how the method watchPosition()works. You will need an accurate GPS device to test this (such as a smartphone):

For example:

Related posts
Other HTML articles
  • YouTube video in HTML

    cách dễ nhất để phát video bằng html là sử dụng youtube.
  • Inserting video in HTML5

    cách chèn video vào html như thế nào? nếu đang muốn tìm hiểu cách chèn video vào website html, mời bạn tham khảo bài viết dưới đây.
  • What is HTML? Basic HTML structure

    html là một phần không thể thiếu khi nói đến lập trình web. mức độ phổ biến của html ngày càng tăng và nó được coi là tiêu chuẩn web chính thức. vậy cụ thể html là gì? html ra đời như thế nào và cấu trúc của html ra sao? hãy cùng tìm hiểu ngay sau đây nhé.
  • How to Learn HTML Language

    html là chữ viết tắt của hyper text markup language (ngôn ngữ đánh dấu siêu văn bản), và là loại mã hay ngôn ngữ được sử dụng để tạo nên các trang web. có thể hơi nản lòng một chút nếu bạn chưa từng viết mã bao giờ trước đây, nhưng tất cả những gì bạn cần để thử làm là một chương trình biên tập văn bản đơn giản và một trình duyệt internet. bạn thậm chí có thể nhận ra vài html được sử dụng để thay đổi kiểu chữ trên các diễn đàn trực tuyến, trong các hồ sơ trực tuyến được tùy biến, hoặc trong các bài viết của tipsmake. html là một công cụ hữu ích cho bất cứ ai sử dụng internet, việc học những thứ căn bản về html có thể mất ít thời gian hơn là bạn nghĩ.
  • How to create a simple contact form using HTML, CSS, and JavaScript

    học cách tạo một biểu mẫu liên hệ cho web thật dễ dàng theo các bước sau, đảm bảo bạn giao tiếp hiệu quả với khách truy cập.
  • Why write neat and organized HTML?

    bạn sẽ nhận được nhiều lợi ích khi viết code html chính xác và gọn gàng. dưới đây là lí do tại sao nên viết code html tối ưu.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup