ios - How to notify nearby users within a certain location -
i've been thinking , couldn't figure out on how develop feature. app technically notify every driver, whenever user trigger button. i'm using google maps calculating routes , on.
the feature i'm building similar uber notification system, whenever user click "set pickup location" notify bunch of drivers nearby user's location.
but problem right now, notify every drivers online, , that's not want. want notify drivers nearby user's location.
how achieve this? sorts of variables need accomplish this
im using
- ios/swift
- google maps
- node.js backend , socket.io realtime
algorithm overview
to notify user near you, suggest following algorithm:
- determine location
- determine location of other users
- calculate distance you
this algorithm simple, heavy, perhaps why @lu_ suggest on server.
optimizations
there multiple hacks optimize it, example, instead of checking location , calculating distance millions of users in app, can elect random subset, or can pick , choose more interesting based on criteria.
steps , code
the first , second steps of algorithm require find locations. effect suggest 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: example requires consent location sharing when // prompted browser. if see error "the geolocation service // failed.", means did not give permission browser // 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: geolocation service failed.' : 'error: browser doesn\'t support geolocation.'); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initmap"> </script> </body> </html>
please note need enable browser give location or website/service (i believe stackoverflow may block it, can check original example working on original page).
once have locations of , users want, can send information client or server. information can calculate distances original client, using information described in discussion calculate distance between 2 latitude-longitude points? (haversine formula) .
with distances, , location of original client, can decide do.
Comments
Post a Comment