How to compare two locations - java

I'm working on a Android app which have feature of nearby friends. So each user have list of friends stored in a Firebase real time database. Also their locations are saved there while they are moving around.
What I want to do?
I want to compare all the user freinds locations with the user location. And if the distance between the user and his friend is one km or less then the nearby friends list will be updated. This check will be done for reach friend for the user that is using the app.
What I tried to?
Before posting this question I searched around and found one answer on Stack Overflow. But I don't get the function which calculates the distance because the variables names are short and not self explanatory.
Based on the written above does anyone knew how can I calculate distance between two location or can summarize what the functions does which is shown on the link above.

You can use distanceBetween() method from the Location class.

firstLocation.distanceTo(secondLocation)
you will get distance from second location to first one in meters.

Related

Calculating distance between places and user location Android Studio

how can I calculate distance between list of locations against users' location and then display the list in a location sorted order stating how many miles the location is from user. Something like below;
I am not sure how I can do this in Java/Android
I understand for this we need to use sorting algorithm, to sort the list in order of nearest distance and write a comparing algorithm that compares given postcodes. I am not sure how I can approach this in Java/Android. I spent time searching online but couldn't find / fit what I found which the goal I am trying to achieve. Any help or guidance would be really nice.
Also, I am not sure how to get miles between the two locations.

In Android how to check if the point from GPS is fitting a given polyline?

I am having a polyline from the Google directions API. I want to make sure that I get rid of the erroneous GPS coordinate values that can come sometimes. For this, I am thinking of fitting each point which I get from the GPS with the present step polyline. Is there any mathematical or in built functions to do this?
The location coordinates which i get from the LocationListener(GPS) need not be always accurate. SOmetimes this can be inside a buidling near the road also. As i already have an accurate polyline from google API which connects two accurate points, i wanted to confirm that each point which get from the GPS is on or very close to this line.
For future reference as the question is quite old, check this
library, its a utility library made for different tasks for android and it has a function in PolyUtil class called isLocationOnPath which takes a LatLng point, a list of points of route and a threshold point for how close you want to check the point for.

How to get nearest places (that are user entered) based on current location?

I am building an Android application where users can find nearest places according to their current location and also, be able to add places into the database. I understand that I will have to use long and lat in my database but I really don't know how to compare the current location against the places in the database. How would I even search it because I would potentially have to go through the entire database systematically pulling out each geo point and then comparing it against the user location?
Example:
Say users have entered 10 places in London and 1000 other places all around the world and a person in London is using the application and wants to find places that are nearest to them based on their current location. How would I search for these 10 places among 1000s of other places in the database? Performing distanceTo() a 1000 times is unnecessary.
Any advice, guides, tutorials, references would be great.
Thanks in advance.
EDIT:
This is my idea so far.
Get the longitude and latitude of the location, which could either have been searched, the current location or the location that the user has tapped on the screen.
With Long/Lat of said location - get the place description of the place from reverse geocoding with the getFromLocation method.
With place description, search database based on address string.
This is what I have right now but again this doesn't seem to be reliable. There must be a simpler way to query the database given the long/lat of a location and return back a complete list of the nearest spots.
If you are using latitude and longitude, this should be pretty simple. Let's say the coordinates of the individual are roughly: 49°N, 2°E.
To find all locations with 1 square degree of lat/long, you would just select all locations that satisfied the following criteria:
Latitude >= 48°N and <= 50°N
.. and ..
Longitude >= 1°E and <= 3°E
It's pretty simple.
There's a table on Wikipedia that explains how much decimal degrees are 1 kilometer etc. And the coordinates that you receive from Google are in decimal degrees.
https://en.wikipedia.org/wiki/Decimal_degrees
If you have any more questions on how to calculate etc, just ask :)
Basically you just need to check if the places latitude is higher than
currentLocationLatitude - rangeInDecimalDegrees
and smaller than
currentLocationLatitude + rangeInDecimalDegrees

How to compare the current location against a list of addresses and come up with nearby addresses within 5 miles/10 miles/20 miles?

How do mobile apps like yelp and gasbuddy find a list of nearby restaurants/gas stations? I am creating an Android app and have a list of locations in my database. I want to give the user all the nearby locations from my database based on the user's current location. What is the best way to go about it? I am using Java.
Some databases actually have this built in; for example here's a StackOverflow question about spatial queries in Postgres, which should set you on the right path. Your app would send coordinates to a server, which would use those coordinates to query a postgres database directly.
This link may be helpful http://www.dreamincode.net/forums/topic/92885-distance-between-2-points/
when you get the distance just compre it with if or whatever you want and then show it or save.

Trilateration example in java

I am currently working on a program that utilizes RSSI to determine location based on signal strength. Does anybody know where a working java example is of trilateration? I couldn't find any online.
The program will basically need to take in 3 distances, which are the distances derived from the RSSI's and then the latitude and longitude of the three points and then determine the user's location using 2 dimensional trilateration (I'm ignoring the height of each beacon for now).
I saw a Python one on here, but I don't know Python well enough to understand it.
For future reference for anybody with the same problem.
I just used the link Girish provided.
http://code.google.com/p/talking-points-3/source/browse/trunk/WifiPosition/src/TalkingPoint/thejoo/Trilateration.java?r=109
but be aware you have to account for when coordinates are coplanar as this isn't accounted for in the code.

Categories