ios - 如何在某个位置通知附近的用户

标签 ios swift google-maps

我一直在思考这个问题,但我想不出如何开发这个功能。目前,只要另一个用户触发按钮,我的应用程序就会在技术上通知每个司机。我正在使用谷歌地图计算路线等。

我正在构建的功能类似于 Uber 通知系统,只要用户点击“设置上车位置”,它就会通知用户位置附近的一群司机。

但现在的问题是,它会通知每个在线的驱动程序,这不是我想要的。我只想通知用户位置附近的司机。

我将如何实现这一目标?我需要什么样的变量来完成这个

我正在使用

  • IOS/swift
  • 谷歌地图
  • node.js 作为后端,socket.io 作为实时

最佳答案

算法概述

为了通知你附近的所有用户,我建议你使用以下算法:

  1. 确定您的位置
  2. 确定其他用户的位置
  3. 计算他们到你的距离

这个算法非常简单,但是非常繁重,这也许就是@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/

相关文章:

android - Pie 及以上版本的 Google map

IOS Google Analytics - 没有数据通过

ios - 实现一个简单的后退按钮 swift ios

ios - UITabBar 中的 UIView 未扩展至完整

ios - 创建一组图像并在 Apple Watch 上随机显示一个

ios - 如何在 swift 3 中从字符串中获取日期?

javascript - 谷歌放置自动完成隐藏键

google-maps - 如何检查谷歌地图视口(viewport)中是否有任何标记?

SwiftUI 使按钮在 HStack 中水平地彼此靠近

swift - Swift 中如何避免内存碎片