-1

It was proposed to develop a page where you present an excerpt of Google Maps with several markers organized in clusters (done). I do not have experience with javascript, and what I need is that clicking a button or not, it is possible to get my location and automatically be informed of which point marked on the map is closer to me. Can someone help me?

      function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 13,
          center: {lat: 40.963308, lng: -8.594651}
        });

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat:  40.962157, lng: -8.593313},
        {lat:  40.962149, lng: -8.595695},
        {lat:  40.960351, lng: -8.598922},   
        {lat:  40.967305, lng: -8.591450},
        {lat:  40.961682, lng: -8.608136}
      ]

    </script>
  • possible duplicate of [Google Maps API - Getting closest points to zipcode](https://stackoverflow.com/questions/17280787/google-maps-api-getting-closest-points-to-zipcode) – geocodezip Oct 18 '17 at 15:49

1 Answers1

0

If you are talking about the aerial distance, use the Euclidean formula on all, pretty fast, considering everything. Just modify it a little, so you don't use the square root, as it is an expensive operation.

(Xpos - Xpoint)^2 + (Ypos - Ypoint)^2

Apply a basic min alogirthm, i.e. always save the lowest distance point.

That's the easy way, generally also the nearest point, if taking places far away.



The way to do the road distance previously would have been to do it one by one, but Google thankfully thought of this case and developed https://developers.google.com/maps/documentation/distance-matrix/intro

I am not sure what the usage quota for your account will be, but that's upto you and your company to figure out depending on the type of plan you want.

thedarkone
  • 402
  • 3
  • 10