Geolocation.getCurrentPosition does not work at night - How to provide my own api key?

This is a long question, perhaps a very short answer.

In my rails 5 application, I use this pretty basic JS implementation of the api geolocation JS to get the location of users from the browser:


UPDATE: Here is the whole script that uses the geocomplete plugin ( https://ubilabs.imtqy.com/geocomplete/ ):

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<%=ENV['GOOGLE_MAPS_API_KEY']%>&language=<%=I18n.locale%>&libraries=places"></script>

<script>
  $(function(){
    var geolocation = null;
    var lati  = null;
    var longi = null;

    //start by drawing a default map
    nogeoloc(); 
    function nogeoloc() {
      geo(lati,longi);
      console.log("no geolocation");
    }

    // check for geolocation support
    if (window.navigator && window.navigator.geolocation) {
      geolocation = window.navigator.geolocation;
    }
    if (geolocation) {
      geolocation.getCurrentPosition(success, error, positionOptions);
    }
    var positionOptions = {
      enableHighAccuracy: true,
      timeout: 10 * 1000, // 10 seconds
    };
    //in case of user permission, center the map at user' location
    function success(position) {
      lati  = position.coords.latitude;
      longi = position.coords.longitude;
      geo(lati,longi);
      console.log("geo data obtained");
    }
    // if denied permission or error, do nothing
    function error(positionError) {
      console.log("error");
      console.log(positionError);
    }

    // map drawing using the geocomplete library
    function geo(lati,longi){
      var myloc = false;
      if(lati && longi) {
        myloc = [lati, longi];
      }
      var options = {
        map: ".map_canvas",
        mapOptions: {
          scrollwheel: true
        },
        location: myloc ,
        details: "form",
        detailsAttribute: "geocompname",
        markerOptions: {
          draggable: true
        }
      };
      $("#geocomplete").geocomplete(options);
      var geocoder = new google.maps.Geocoder();
      var map = $("#geocomplete").geocomplete("map");
      var marker = $("#geocomplete").geocomplete("marker");

      if(lati && longi) {
        map.panTo({ lat: lati, lng: longi});
        marker.setPosition({ lat: lati, lng: longi});
        $("input[geocompname=lat]").val(lati);
        $("input[geocompname=lng]").val(longi);  
        geocoder.geocode({'location': {lat: lati, lng: longi}}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
              $("input[geocompname=formatted_address]").val(results[0].formatted_address);
            }
          }
        });
      }
      $("#geocomplete").bind("geocode:dragged", function(event, latLng){
        $("input[geocompname=lat]").val(latLng.lat());
        $("input[geocompname=lng]").val(latLng.lng());
        map.panTo(latLng);
        geocoder.geocode({'latLng': latLng }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
              $("input[geocompname=formatted_address]").val(results[0].formatted_address);
            }
          }
        });
      });
    }
  });
</script>

Somehow ridiculous, the problem is that this implementation works fine during the day (I'm based in Germany), but at night it returns an error in both Firefox and Chrome :).

Error code returned in Firefox: { code: 2, message: "Unknown error acquiring position" }Chrome prints additional information:message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 403."

, 403 , api. , :).

, firefox google goi geo.wifi.uri voilà, .

():

1) Google (Chrome), Mozilla (https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation), google/mozilla .., , ?

2) . google api, ?

3) Chrome Samsung s6, , - Samsung. , ?

+4
2

geolocation.watchPosition , ?

+1

-? (, , ..)? . , API Google Maps Google Places. . : https://developers.google.com/maps/pricing-and-plans/. , Google ( , , .. )

+1

Source: https://habr.com/ru/post/1676319/


All Articles