Home  >  Article  >  Web Front-end  >  How to locate user's position in HTML?

How to locate user's position in HTML?

王林
王林forward
2023-08-20 21:45:352259browse

How to locate users position in HTML?

Sometimes the task is to find the current location of the user and then display the location coordinates on a web page or display the location on a map. Using HTML and javascript code, this article demonstrates the process of getting the user's current location in an HTML page. Demonstrated by using two different examples. In the first example, you can get the user's current location and then display the location coordinates on an HTML page. In the second example, the open source Leaflet map library is used to obtain and display the user's current location on a map.

Example 1: Find the user's current location and display the location coordinates on the html page.

Code Explanation and Design Steps

Step 1 − Create an HTML file and start writing code.

Step 2 - Create a label

and set it to display the location coordinates.

Step 3 - Create a button and call the function getYourLoc() when the button is clicked.

Step 4 - Write code in the <script> tag and create two functions getYourLoc() and showMyPos(pos). </script>

Step 5 − Write the javascript code in getYourLoc() to find the current location using navigator.geolocation.getCurrentPosition() and then call showMyPos(pos).

Step 6 - Add the position to the

tag created earlier, using showMyPos(pos). test result.

Important file used − locationfile.html

The Chinese translation of

Code For parentFolderFile.html:

is: The code of

parentFolderFile.html:

<!DOCTYPE html>
<html>
   <body>
      <p>Show My Location Coordinates</p>
      <button onclick="getYourLoc()">Get the location coordinates</button>
      <p id="showloc"></p> 
      <script>
      var x = document.getElementById("showloc");
      function getYourLoc() {
         if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showMyPos);
         } else {
            x.innerHTML = "No support for this in the browser.";
         }
      }
      function showMyPos(pos) {
         x.innerHTML = "My Latitude is : " + pos.coords.latitude +
         "<br>My Longitude is : " + pos.coords.longitude;
      }
      </script>
   </body>
</html>

Viewing The Result

To see the results, open the loactionfile.html file in your browser. Now click on the button to find the user's current location. The coordinates will be displayed on the html page.

Example 2: Find the user's current location and display the location coordinates on the map.

Code Explanation and Design Steps

Step 1 − Create an HTML file and start writing code.

Step 2 - Create a label

and set it to display the location coordinates.

Step 3 - Create a button and call the function getYourLoc() when the button is clicked.

Step 4 − Inside the <script> tags write the code and make three functions getYourLoc() , showMyPos(pos), and showmap(pos).</script>

Step 5 − Write the javascript code in getYourLoc() to find the current location using navigator.geolocation.getCurrentPosition() and then call both the functions showMyPos(pos) and showmap(pos).

Step 6 - When displaying the map in the function showmap(pos), include the leaflet library, use the base map, set the marker and pass the current location coordinates to mark the map location.

Step 7 - Use showMyPos(pos) to add the location to the

tag created earlier. Also displays the map. test result.

Important file used − locationfile11.html

Code For locationfile11.html:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <title>Leaflet Map</title>
   <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin="" />
   <style type="text/css">
      body{
         margin: 0;
         padding: 0;
      }
      #map {
         width: 100vw;
         height: 100vh;
      }
   </style>
</head>
<body>
   <p>Show My Location Coordinates</p>
   <button onclick="getYourLoc()">Get the location coordinates</button>
   <p id="showloc"></p>
   
   <div id="map"></div>
   // the leaflet library for making maps
   
      <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>
      <script>
      var x = document.getElementById("showloc");
      // get the current location of the user
      function getYourLoc() {
         if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showMyPos);
         } else {
            x.innerHTML = "No support for this in the browser.";
         }
         if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showmap);
         } else {
            x.innerHTML = "No map here.";
         }
      }
      function showMyPos(pos) {
         var lat=pos.coords.latitude;
         var lon=pos.coords.longitude;
         x.innerHTML = "My Latitude is : " + lat +
         "<br>My Longitude is : " + lon;
      }
      function showmap(pos){
         var latc=pos.coords.latitude;
         var lonc=pos.coords.longitude;
         const map = L.map('map', {
            center: [latc, lonc],
            zoom: 3
         });
         //Adding a base map
         L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }).addTo(map);
         const marker1 = L.marker([latc, lonc]).addTo(map);
      }
   </script>
</body>
</html>

Viewing The Result

For seeing the result open the loactionfile11.html in a browser. Now click the button to find the current location of the user. The coordinates are seen and marked on the map.

In this HTML article, two different examples are used to demonstrate how to find the user's current location. First, a method is introduced to display the location coordinates on the page after obtaining the location, and then a method of using these coordinates to display on the map while displaying the location in the HTML page is introduced.

The above is the detailed content of How to locate user's position in HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete