J
J
Jekins D&D2016-07-27 01:17:34
Google Maps
Jekins D&D, 2016-07-27 01:17:34

How to get user's geolocation without https?

There is a script provided on the Google site to determine the user's geolocation using the html 5 api:

<script>
      // 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.

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 6
        });
        var infoWindow = new google.maps.InfoWindow({map: map});

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

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

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
    <p><button onclick="geoFindMe()">Show my location</button></p>
<div id="map"></div>


But it only works if the site has https. Is it possible to get geolocation on a site with http?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Назар Мокринский, 2016-07-27
@Jekins

Нет, только пробовать угадать страну по IP, ничего более точного в общем случае не получится.
Локально на localhost геолокация должна работать для разработки, в продакшене исключительно HTTPS. Уважайте своих пользователей.
Бесплатный сертификат можно получить, например, у Lets Encrypt и StartSSL.

V
Valeriyat, 2017-01-21
@Valeriyat

If you only need Russia, try Dadata

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question