dimanche 18 avril 2021

google maps json api value comes undefined

I want the location of the user on my website when loading the page;

if(navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(
        (position) => {
        const pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
        };
      ..............
}

In the first case, I send the values ​​from the json api to the function as true-false whether they are in the circle that takes the position to the center. If true I add element to array

$.getJSON("https://example.com//json/", function (data) {
              var count = Object.keys(data).length;  
              for (var i = 0; i < count; i++) {
                      var lat = data[i].LAT;
                      var lng = data[i].LNG;
                      var locs = { lat: lat, lng: lng };
                      var check = arePointsNear(pos, locs, j);
                      if (check) {
                          start++;
                          if (start == 1) listStations.push(data[i].ID);
                          var restartValue = listStations.indexOf(data[i].ID);
                          if (restartValue < 0) listStations.push(data[i].ID);
                      }
             }
}

Every time the page is refreshed, it automatically enters this if (if allowed) but I cannot access the items of the directory (listStations) in the gene except json.

Alternatively, I tried to add a button and give an onclick event. The function did not work.




Aucun commentaire:

Enregistrer un commentaire