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 = [
                ["Germany, Czech Republic"],
                ["Czech Republic"],
                ["Prague, Czech Republic"],
            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
  • The answer was posted here answer
    – Rolly
    Sep 27, 2017 at 23:25

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.