2015-05-29 184 views
0

如何根据给定距离(以公里为单位)从经度和纬度的另一个城市过滤给定的经度和纬度城市列表。过滤城市的经纬度与其他城市的距离(经纬度)?

我想使用Google Maps API(javascript)或其他适合的解决方案。

cities = { 
      ['zürich','47.368650', '8.539183'], 
      ['City2','21.568650', '2.439183'], 
      ['City3','41.568650', '4.439183'] 
} 


distance = 2; //2 km 
targetCity = ['zürich','47.368650', '8.539183']; 

需要:getNearCities(targetCity,distance,cities);

谢谢

+0

你可以看到这个页面http://www.movable-type.co.uk/scripts/latlong上haversine公式。 html –

+0

查看google距离矩阵api https://developers.google.com/maps/documentation/distancematrix/ – Ronin

回答

0

时退房google.maps.geometry.spherical namespace创建LatLng对象。用它你可以得到两点之间的距离。

var origin = new google.maps.LatLng(###,###); 
var destination = new google.maps.LatLng(###,###); 
var maxDistance = 2; //in km 

var withinRange = (google.maps.geometry.spherical.computeDistanceBetween(origin, destination) <= maxDistance); 
0

幸得http://www.geodatasource.com/developers/javascript

var cities = [ 
    ['zürich', 47.368650, 8.539183], 
    ['City2', 21.568650, 2.439183], 
    ['City3', 41.568650, 4.439183] 
]; 

var dist = calculateDistance(cities[0][2], cities[0][3], cities[1][4], cities[1][5], 'K'); 
alert(dist); 

function calculateDistance(lat1, lon1, lat2, lon2, unit) { 
    var radlat1 = Math.PI * lat1/180 
    var radlat2 = Math.PI * lat2/180 
    var radlon1 = Math.PI * lon1/180 
    var radlon2 = Math.PI * lon2/180 
    var theta = lon1 - lon2 
    var radtheta = Math.PI * theta/180 
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); 
    dist = Math.acos(dist) 
    dist = dist * 180/Math.PI 
    dist = dist * 60 * 1.1515 
    if (unit == "K") { 
     dist = dist * 1.609344 
    } 
    if (unit == "N") { 
     dist = dist * 0.8684 
    } 
    return dist + unit 
} 

JsFiddle

相关问题