ONE NET WIKI Free and professional IT Wiki

How to show current location on a map by Google Maps API

2018-09-10 ONE NET WIKI


Geolocation is a navigation to your current position on the map.

When you open the Google Maps, it asks you if you allow them to know your location. Once you accept, the map will show your current location.

It is really useful in case of driving direction because you can share your location and just insert the destination point.

The entire test code has been copied from the developer's website of Google.

<!DOCTYPE html>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    <div id="map"></div>
      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.
      var map, infoWindow;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 6
        infoWindow = new google.maps.InfoWindow;

        // Try HTML5 geolocation.
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude

            infoWindow.setContent('Location found.');
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');;
    <script async defer



The function initMap() is used to generate our map.


The function getCurrentPosition will read the position of our device and give it to the special variable with coordinates.


Handle the info window and center the map

  • infoWindow.setPosition(pos)

    Sets position of the window with information if the geolocation was successfule.

  • infoWindow.setContent('Location found.')

    Set the description of the window.


    Object on which the window will be opened.

  • map.setCenter(pos)

    If a user allows the geolocation, the map will be centered on his current location.



The function handleLocationError() is called when the geolocation feature deos not work.

 The error information will return when the user blocks the geolocation feature or when the browser which he is using does not support geolocation.


Editor's Note: All the articles in this website are original. If reproduced, please indicate the source and link.
All Comments (0)
Expand to publish a comment

Web Development

Web Management

Web Marketing

Web Security