[GIS] Google maps API find the nearest markers in the location

google-maps-apijavascript

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));
            }
        }

Best Answer

I found your question while looking to do something similar. If I understand you correctly then there is a promising article at https://developers.google.com/maps/articles/phpsqlsearch_v3 that might help. The article uses PHP, MySQL and XML but it illustrates the principles required i.e. how to use the Haversine formula with latlngs stored in a SQL datasource to calculate the 'closest to' markers. I haven't fully worked through it yet but it seems fairly straightforward.