1

I have a leaflet map. In the map i have a google search where you type a location name and then the map zooms and put a marker according to the location. I also have a database table with user ids and their lat/lng .

Now what i want to do is to set in a variable the lat/lng of the marker of google search. Then do a query at my database and get all the users lats/lngs . And if a user is close to the google's marker's lat/lng display him on the map as a custom marker. (if he is close will be calculated with the distanceTo() function) i have knowledge of the php query but i stuck in javascript

how do i continue from the google search marker? How i set a variable with his lat/lng?

javascript :

// google location search
 var GoogleSearch = L.Control.extend({
      onAdd: function() {
        var element = document.createElement("input");

        element.id = "searchBox";

        return element;
      }
    });
(new GoogleSearch).addTo(mymap);

    var input = document.getElementById("searchBox");

    var searchBox = new google.maps.places.SearchBox(input);

    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      var group = L.featureGroup();

      places.forEach(function(place) {

        // Create a marker for each place.
        console.log(places);
        console.log(place.geometry.location.lat() + " / " + place.geometry.location.lng());
        var marker = L.marker([
          place.geometry.location.lat(),
          place.geometry.location.lng()
        ]);
        group.addLayer(marker);
      });

      group.addTo(mymap);
      mymap.fitBounds(group.getBounds());

    });
  • Thought: use the db query to limit who's closeby, instead of loading all the users and looking at distance. – barrycarter Sep 9 '17 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.