// 模拟商家位置数据let merchants = [
{ name: 'XX 餐厅', latitude: 30.123, longitude: 120.456 },
{ name: 'YY 商店', latitude: 30.234, longitude: 120.567 }];// 获取附近商家位置的函数function getNearbyMerchants(userLatitude, userLongitude) {
let nearbyMerchants = [];
for(let merchant of merchants) {
// 计算距离
let distance = calculateDistance(userLatitude, userLongitude, merchant.latitude, merchant.longitude);
if (distance < 5) { // 假设 5 公里内为附近
nearbyMerchants.push(merchant);
}
}
return nearbyMerchants;}function calculateDistance(lat1, lon1, lat2, lon2) {
// 简单的距离计算示例,实际应用中需要更精确的算法
let R = 6371; // 地球半径(千米)
let dLat = (lat2 - lat1) * Math.PI / 180;
let dLon = (lon2 - lon1) * Math.PI / 180;
let a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon/2) * Math.sin(dLon/2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
let d = R * c;
return d;}// 假设用户当前位置let userLatitude = 30.150;let userLongitude = 120.500;console.log(getNearbyMerchants(userLatitude, userLongitude));
暂时没有评论,来抢沙发吧~