我一直在思考这个问题,但我想不出如何开发这个功能。目前,只要另一个用户触发按钮,我的应用程序就会在技术上通知每个司机。我正在使用谷歌地图计算路线等。
我正在构建的功能类似于 Uber 通知系统,只要用户点击“设置上车位置”,它就会通知用户位置附近的一群司机。
但现在的问题是,它会通知每个在线的驱动程序,这不是我想要的。我只想通知用户位置附近的司机。
我将如何实现这一目标?我需要什么样的变量来完成这个
我正在使用
- IOS/swift
- 谷歌地图
- node.js 作为后端,socket.io 作为实时
最佳答案
算法概述
为了通知你附近的所有用户,我建议你使用以下算法:
- 确定您的位置
- 确定其他用户的位置
- 计算他们到你的距离
这个算法非常简单,但是非常繁重,这也许就是@LU_ 建议你在你的服务器上这样做的原因。
优化
有多种 hack 可以优化它,例如,您可以选择一个随机子集,而不是为您的应用程序中的所有数百万用户检查位置和计算距离,或者您可以挑选更有趣的人基于一些标准。
步骤和代码
此算法的第一步和第二步要求您找到位置。为此,我建议您使用以下示例:
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({
map: map
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
</body>
</html>
请注意,您需要启用浏览器以提供其位置或网站/服务(我相信 StackOverflow 可能会阻止它,但您可以查看原始页面上的原始示例)。
一旦您获得了您和您想要的用户的位置,您就可以将此信息发送到客户端或服务器。有了这些信息,您就可以使用本讨论中描述的信息计算到原始客户端的距离 Calculate distance between two latitude-longitude points? (Haversine formula) .
根据距离和原始客户的位置,您可以决定要做什么。
关于ios - 如何在某个位置通知附近的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631789/