我认为您应该能够根据您的目的调整此答案。
//forumla for calculating distances between points
var getDistance = function(p1, p2) {
var rad = function(x) {return x*Math.PI/180;}
var R = 6371; // earth's mean radius in km
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.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;
return d.toFixed(3);
}
// LatLng object with your current coordinates
var yourLocation = new google.maps.LatLng(yourlat, yourlng);
// populate an array of hard coded LatLng objects
var twentypoints = {};
twentypoints['hardcodedName1'] = new google.maps.LatLng(lat1, lng1);
twentypoints['hardcodedName2'] = new google.maps.LatLng(lat2, lng2);
twentypoints['hardcodedName3'] = new google.maps.LatLng(lat3, lng3);
...
// calculate the minimum distance
var closestLocName, minDist;
for (var hardcodedName in twentypoints) {
tempMinDist = getDistance(twentypoints[hardcodedName], yourLocation));
if (minDist === undefined || tempMinDist < minDist) {
minDist = tempMinDist;
closestLocName = hardcodedName;
}
}
console.log('Closest location is called ' + closestLocName + '.');
console.log('Closest location is ' + minDist + 'km away.');