0

I've been thinking about this and I couldn't figure it out on how to develop this feature. Currently my app will technically notify every driver, whenever another user trigger a button. I'm using google Maps for calculating routes and so on.

The feature that I'm building is similar like Uber notification system, where whenever a user click "set pickup location" it will then notify to bunch of drivers nearby the user's location.

But the problem right now, it will notify to every drivers that is online, and that's not what I want. I want to notify only the drivers that are nearby to the user's location.

How would I achieve this? What sorts of variables do i need to accomplish this

Im using

  • IOS/Swift
  • Google maps
  • node.js as the backend and socket.io as realtime
airsoftFreak
  • 1,450
  • 5
  • 34
  • 64
  • 1
    this is a thing that backend should take care of, from app you just send your location, backend check other locations and pick any that is in range – Lu_ Jul 28 '16 at 09:16

1 Answers1

1

Algorithm overview

To notify all the user that are near you, I suggest you the following algorithm:

  1. Determine your location
  2. Determine the location of other users
  3. Calculate their distance to you

This algorithm is very simple, but very heavy, perhaps why @LU_ suggest you do this on your server.

Optimizations

There are multiple hacks to optimize it, for example, instead of checking the location and calculating the distance for all the millions of users in your app, you can elect a random subset, or you can pick and choose who is more interesting based on some criteria.

Steps and Code

The first and second steps of this algorithm require you to find locations. For this effect I suggest you the following example:

<!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>

Please note that you need to enable your browser to give its location or website/service (I believe StackOverflow may block it, but you can check the original example working on the original page).

Once you have the locations of you and the users you want, you can send this information to the client or to the server. With this information you can then calculate the distances to the original client, using the information described in this discussion Calculate distance between two latitude-longitude points? (Haversine formula) .

With the distances, and the location of the original client, you can then decide what to do.

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266