Calculating distance between places and user location Android Studio - java

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.

Related

How to compare two locations

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.

Calculate distance in openstreetmap with josm?

I'm new to openstreetmap. I want to use openstreetmap in a java application to get distance between two points. And also to find more information about a particular road, etc.
I discovered JOSM, but all I need is to calculate distance between two points in a road, also get some more information about that road, when necessary. Anybody knows which class/methods in JSOM will help me do that? Or is there any other simpler method/api to do that with OpenStreetMap?

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 calculate all points(longitude,latitude) within a given radius from given point (longitude,latitude)?

I have a given point (longitude,latitude) and I want to get all the points ranges that comes lets say 5 miles radius in given point?
I'm just guessing here, but I think you'll need to find a different approach. If you're trying to do something like Foursquare, Google Maps, etc where it finds places within a 5-mile radius of your current location, I think you'll find that these services don't calculate all the points in that radius and then match them up to places at those points.
There would probably be some smarts behind the code that do something like this...
Get the users current location
Find the suburb (or failing that, find the city) that the current location exists in. Also find all the surrounding suburbs adjacent to this one.
Find all the places within those suburbs, and calculate how far they are away from the current user location
This kind of process is one potential method that could be employed by these services. This deals with small subset of place comparisons, which is relatively quick to perform. Also, places on a map usually have a suburb/city associated with them anyway, so database lookups for places would be rather quick, as there would be an index that involves the suburb.
If you're aim is to do something like this, I would try to figure out a different way to compare points rather than simply trying to calculate everything in your radius.
And of course, there would also be plenty of specific algorithms for calculating this better, but that's not my area of expertise, and would be better suited to another forum. I'm not trying to say that this is the best way to do it, but there's plenty of other ways to do it that rely on known location data which would be quicker and smarter than your suggested requirement.

location based data grabbing algorithm

I have to develop an algorithm for location based data grabbing and displaying it on a mobile device. I'm just a web programmer with some knowledge of php and mysql. I need to develop this algorithm. Please give some instructions on where to start the development.
You could have PointOfInterest table that relates to address.
Let say I want to find an ATM in Chicago. Then you perform a seach for all ATMs in chicago.
After you get your result set you perform distance calculation over each of them and check which is the closest one.
Here you may find a way how to calculate distance based on longitude/latitude coordinates
http://www.movable-type.co.uk/scripts/latlong.html

Categories