gpt4 book ai didi

javascript - 查找距离我当前位置最近的 friend

转载 作者:行者123 更新时间:2023-12-03 02:53:26 25 4
gpt4 key购买 nike

我想通过手机获取距离我当前位置较近的 friend 的位置来找到他们。例如,在下面的代码中,我有 var 城市,如果我输入 3,4 个 friend 的号码,我可以这样做吗?或者我可以做一些其他改变吗?可能吗?

// Get User's Coordinate from their Browser
window.onload = function () {
// HTML5/W3C Geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(UserLocation);
}
// Default to Washington, DC
else
NearestCity(38.8951, -77.0367);
}

// Callback function for asynchronous call to HTML5 geolocation
function UserLocation(position) {
NearestCity(position.coords.latitude, position.coords.longitude);
}


// Convert Degress to Radians
function Deg2Rad(deg) {
return deg * Math.PI / 180;
}

function PythagorasEquirectangular(lat1, lon1, lat2, lon2) {
lat1 = Deg2Rad(lat1);
lat2 = Deg2Rad(lat2);
lon1 = Deg2Rad(lon1);
lon2 = Deg2Rad(lon2);
var R = 6371; // km
var x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
var y = (lat2 - lat1);
var d = Math.sqrt(x * x + y * y) * R;
return d;
}

var lat = 20; // user's latitude
var lon = 40; // user's longitude

var cities = [
["city1", 10, 50, "blah"],
["city2", 40, 60, "blah"],
["city3", 25, 10, "blah"],
["city4", 5, 80, "blah"]
];

function NearestCity(latitude, longitude) {
var mindif = 99999;
var closest;

for (index = 0; index < cities.length; ++index) {
var dif = PythagorasEquirectangular(latitude, longitude, cities[index]
[1], cities[index][2]);
if (dif < mindif) {
closest = index;
mindif = dif;
}
}

// echo the nearest city
alert(cities[closest]);
}

最佳答案

我建议使用GeoFire为此,它来自 firebase,并具有基于地理的查询,并且可以以简单的方式执行您想要的操作

关于javascript - 查找距离我当前位置最近的 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com