问题
I tried searching but i couldn't find anything. My Question is "How can i alert 2 or more users if they are nearby each other?" in android using Geo-fencing or something else.
Say, If a UserA is in football ground and UserB walking nearby that football ground. Then UserA and UserB automatically gets notification that UserA/UserB are somewhere nearby.
回答1:
Finally after spending a couple of hours thinking, I thought a better way to do this:
Setup a database (MySQL,SQL etc) in your server which have users table and location table which have the locations data
In Android create a Service which will fire once in 15 minutes requesting current location.
Create SharedPreference / SQliteDB which holds the last Coordinates of the device which was also updated to the Server database.
3.1
If
the current location matches the Last Location retrieved from the SharedPreferenceand/or
current location withinx
proximity (depends on how much you givex
30ft or more)then
user is in the same place so don't upload the coordinates to the Server Database.3.2.
If
the user isn't within proximityor
last location doesn't match current locationthen
upload the coordinates to the Server Database.After uploading coordinates to the server, update SharedPreference too...
After uploaded coordinates, The Server must return the User details from its database who are within your proximity. Your app then knows who are near you and send notification to you saying "Your buddy XXX is nearby!"
In Server, after uploading your coordinates, the server will also send notification to the user who are nearby you using Google Cloud Messaging.
Tips: Run a cron job in your server which will execute every minute and checks "Who is near to Who" and send notification to them.
Hope it helps somebody :)
回答2:
It's too simple. Just send your mobile location to the database and use this query while searching for nearby users:
$query = "select * from
(SELECT name,latitude,longitude,round((((acos(sin((".$latitude."*pi()/180)) *
sin((`latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`latitude`*pi()/180)) *
cos(((".$longitude."- `longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344),1) as distance
FROM location) as temp order by distance";
For more info I have made a post on that.
http://www.freebieslearning.info/android-2/android-find-nearby-user/
I hope this will help you.
回答3:
- On A device query your own position trough android location api.
- On A device send the result to your friends
- On B device receive the Device A location
- On B device send the Device B location to Device A.
- Calculate the distances between A and B. Display notification if distance requirements were met.
There is a lot o ways of how to do it.
来源:https://stackoverflow.com/questions/26258697/alert-when-two-users-friends-are-near-to-each-other-android-proximity