使用地理定位选择 REST 服务器
Selecting a REST server using Geolocation
假设在新加坡加载了一个使用休息服务的单页应用程序,并且有两个可能的位置托管应用程序使用的 REST/边缘服务,一个在纽约,一个在新加坡。有谁知道 javascript 框架可以让应用程序 select 最近的数据中心(在本例中是新加坡)?
- 应用程序加载在 http rest 调用中获取数据中心经纬度。
- 通过 Cordova 地理定位或任何其他插件获取您当前设备的经纬度位置
使用此算法计算设备本身在所有数据中心列表和设备当前位置之间的距离,然后列出距离并选择最短的距离
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
假设在新加坡加载了一个使用休息服务的单页应用程序,并且有两个可能的位置托管应用程序使用的 REST/边缘服务,一个在纽约,一个在新加坡。有谁知道 javascript 框架可以让应用程序 select 最近的数据中心(在本例中是新加坡)?
- 应用程序加载在 http rest 调用中获取数据中心经纬度。
- 通过 Cordova 地理定位或任何其他插件获取您当前设备的经纬度位置
使用此算法计算设备本身在所有数据中心列表和设备当前位置之间的距离,然后列出距离并选择最短的距离
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { var R = 6371; // Radius of the earth in km var dLat = deg2rad(lat2-lat1); // deg2rad below var dLon = deg2rad(lon2-lon1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; // Distance in km return d; } function deg2rad(deg) { return deg * (Math.PI/180) }