سؤال

I am developing a business directory site whose search will be driven by Google Maps. Users will be able to search for businesses in their area based on various criteria, but mainly the idea is that if you search for e.g. "plumbers in New Jersey", you'll get a result of all plumbers in New Jersey. However, if you search for "plumbers in Jersey Shore", you should get only the plumbers that operate in Jersey Shore, which for this example would be a suburb or other type of sub-division of greater New Jersey. As an aside, I'm stripping out "plumbers in", and only passing the actual geographic search term, so my actual google maps search is only "New Jersey" or "Jersey Shore". So don't focus on the actual search text itself.

This is my search snippet:

var latlng = results[0].geometry.location;//results is google maps search result for address
console.log(results[0]);
map.setCenter(latlng.lat(), latlng.lng());
map.addMarker({
    lat: latlng.lat(),
    lng: latlng.lng(),
    icon: 'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png'
});

var closestmarkers = [];
var MAXDISTANCE = 5;//radius of our search area in km
closestmarkers = find_n_closest_markers(latlng, n, MAXDISTANCE);//see below snippet for function definition

This is the javascript that figures out which markers are closest to the searched :

function find_n_closest_markers(latlng, n, MAXDISTANCE){
    var lat = latlng.lat();
    var lng = latlng.lng();
    var R = 6371; // radius of earth in km
    var distances = [];
    var closest = -1;
    for (i = 0; i < map.markers.length; i++) {
        var mlat = map.markers[i].position.lat();
        var mlng = map.markers[i].position.lng();
        var dLat = rad(mlat - lat);
        var dLong = rad(mlng - lng);
        var a = Math.sin(dLat / 2)
              * Math.sin(dLat / 2)
              + Math.cos(rad(lat))
              * Math.cos(rad(lat))
              * Math.sin(dLong / 2)
              * Math.sin(dLong / 2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c;

        distances[i] = { distance: d, index: i };
    }
    distances.sort(comparedistances);

    //remove all distances greater than MAXDISTANCE
    toofarindex = -1;
    for (j = 0; j < distances.length; j++)
    {
        if(distances[j].distance >= MAXDISTANCE)
        {
            toofarindex = j;
            break;
        }
    }

    if (toofarindex != -1)
    {
        distances = distances.splice(0, toofarindex - 1);
    }

    //return first n + 1 items(the closest marker is the one indicating the user's current/preferred location. which doesn't count)
    if (distances.length > n + 1)
    {
        distances = distances.splice(0, n + 1);
    }
    return distances;
}

I am not concerned about the actual function. That works 100%. What I'm looking for is how to figure out, based on the search term, what the value of MAXDISTANCE should be. 5 is just a good compromise constant for now, but I need to know that New Jersey is e.g. 20 miles in diameter, whereas Jersey Shore is only 5 miles (those figures come straight out of my ear, not an actual map).

هل كانت مفيدة؟

المحلول

The geocoder also returns a viewport and a bounds for the result. If you need a diameter, you can convert one of those to a distance (the width or the height of the bounds will give you a diameter, if not use that bounds to bound your search.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top