Google maps api find the nearest markers in my location

I have this code and I don’t know how to include “find the nearest markers in my location”.

I tried some codes but nothing didn’t work perfectly.

I want if i click to button it shows markers in my location and remove all other markers. Is it possible?

function initMap() {
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 3,
              center: {lat: 49.7437895, lng: 15.3360726}
            });

            var infoUser = [
                ["Italy"],
                ["Germany, Czech Republic"],
                ["Czech Republic"],
                ["Poland"],
                ["Prague, Czech Republic"],
                ["Italy"]
            ] 
            var markers = [];
            for (var x = 0; x < infoUser.length; x++) {
              var adresa = infoUser[x][0];
              $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+adresa, null, 
                function (adresa) { 
                    return function(data){
                    var p = data.results[0].geometry.location;
                    var latlng = new google.maps.LatLng(p.lat, p.lng);
                    var marker = new google.maps.Marker({
                      position: latlng,
                      title: adresa,
                      map: map
                    }); 
                    markers.push(marker); 
                  }
              }(adresa));
            }
        }

It is certainly possible. You can add a listener to each marker then interate the markers and call the setMap(null) or setMap(map) method on the markers to hide or show the markers.

Adding libraries=geometry parameter to your Google maps script line will add in some spherical functions you can use for distance calculations to determine which markers to show or hide.

/d

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.