Find custom near locations - android

I have a table with the following fields:
Name Longitude Latitude
The user can search for near places depending on the radius he chooses, what is the algorithm used to do this?
Example: I retrieve the user Longitude/Latitude, and I ask him about the radius in KM he wants to apply to his search, then these 3 parameters should be sent to the method that will retrieve the location.

I found the answer.
In other words what we need is the distance between two points that we have their coordinates, so using math :
Distance between two points

Related

Add 5 kilometer to latitude and longitude

I am facing a problem I don't how to do that. Suppose I have 20 users in the city so I want to find the nearest user to me with in a range of 5 kilometres. Actually, I am building an app using firebase where I can find the nearest blood donor. How can I find it? Need suggestion. if I add 5 kilometres to latitude and longitude from all side. Is it possible and Am I on the right track? if yes so how would I add 5 kilometres to latitude and longitude?
You can use the geodesy library to calculate distances between locations.
https://pub.dev/packages/geodesy
num distance = geodesy.distanceBetweenTwoGeoPoints(l1, l2);
Note -- since the earth's surface is not flat (although some still believe that) you cannot use simple euclidian geometry to calculate the distance.

Android Google Map Finding Nearby Pre-Listed Locations

I have an ever increasing list of places identified by lat/long stored in my database. Now at the UI front there is a screen with a place auto-complete textbox means if I type a location there a drop down will appear with places and when I select the place the camera will move there. Also it is required that I display locations nearby (locations as stored in my database). Now the question is how to do this? I can use
SphericalUtil.computeDistanceBetween()
But the problem is I can't fetch all lat/long from my database and calculate distance without hampering performance.
You would need to compute the distances in your database. There are two basic options:
Implement your own distance method in your database:
If your positions are close to each other and close to the test lat/lng you can use Pythagoras's theorem (a ver basic approach that will not be correct in all the cases)
To address all the possible cases you can implement yor own haversine formula (the haversine formula gives great-circle distances between two points on a sphere from their longitudes and latitudes and is used to compute distances in the SphericalUtil.computeDistanceBetween() method).
Use a GIS database. You can use SpatiaLite for Android and use the ST_Distance function to filter your positions:
SELECT *
FROM yourtable
WHERE ST_DISTANCE(Geometry, MakePoint(yourXcoordinate, yourYcoordinate)) < yourdistance

How to calculate distance between two areas in a city

I wanted to make an android app which provides the distance between two areas in a city.
The user will have two input fields where he must enter the two area names he would like to find the distance.
After he enters the area name. My app should display the distance.
How should I implement this?
First of all you need to find latitude and longitude of that area name.
Convert both address in Location object, refer this.
Calculate distance by using distanceTo()
location1.distanceTo(location2);
Returns the approximate distance in meters between this location and
the given location.
refer this for distanceTo() detail.
First get latitude and longitude of your start point and end point and using
distanceBetween method of Location class calculate the distance here is link
http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,%20double,%20double,%20double,%20float[])
Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.
For example, directions from Montreal to Toronto:
GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false
You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
You can also get the polyline information directly from the response object.
for more detail visit here. Get the distance between two locations in android?

How to find a current position is between the two Latitude and Longitude?

I have a SQLite database which stores latitude and longitude. I have to find if the users current location is between the Latitude and Longitude(Which is stored in Database) or not.
I been looking for a while to find a solution for this problem but couldn't get it. Most of them are related with the distance calculation.
How to find the current location(obtained in the app) is between the Latitude and Longitude(Which is stored in SQLite Database)?
I am not sure if you exactly want but what i think
what you have : two geographical point (two pair of attitude and longitude )
what you want : calculate the adjoining regions covered between the two geo points , and check if the user is within that are
for that what you can do is :
1 : suppose the two points are diameteric terminals of a circle and search if the user is within this circle , like this Image
this approach might make you search a little irrelavant region
2 : suppose the two point are two ends of a straight line , consider a distance , say 50 km , on both sides , and search the 100 km wide strip from point A to point B
like this Image
Assume you have two locations a and b and your current location c.
C
/ \
A---B
You can use the dotproduct to calculate if c is between a and b:
If the dotproduct of two vectors is >0, the degrees between these vectors is <90°.
Know you need two calculations like this:
if(Vector(AB) * Vector(AC) > 0 && Vector(BC) * Vector(BA) > 0)
// your location is between
You can also look here and here for more details.

Get all items around a certain point from SQLite Database

I have a database full of map Locations (Latitude, Longitude), is there a way I could do a SQL query to return me all points around a certain location?
Right now I basically grab everything in the database and loop through each one and check the distance using
Location.distanceBetween(startPoint.latitude, startPoint.longitude,endPoint.latitude,endPoint.longitude, results);
and keeping items that are within the set distance but there could be a lot of points to loop through.
So is there anyway to do this in an SQL statement?
You can use a WHERE clause expression to filter on angular distance r from an origin (x0, y0). Since SQLite doesn't have a square root function, you'll have to use the squared distance:
SELECT ... FROM ...
WHERE (Latitude-x0)*(Latitude-x0) + (Longitude-y0)*(Longitude-y0) < r*r;
The only place this won't work well is near the poles or the prime meridian. It's also a planar approximation to the sphere, so it will only work for values of r that are quite small. Finally, it scales latitude and longitude equally, so the selected region looks more and more elliptical the farther away the origin is from the equator.
You will have to convert linear distance (e.g., "within 30 meters") to latitude/longitude differences. This is a rather complex subject because the Earth is not a perfect sphere. However, for rough calculations you can use the approximation that 1 nautical mile = 1852 meters = 1 arc minute of longitude at the equator. Since lines of longitude get closer together as the latitude moves away from the equator, you will need to use some trig to figure out what value of r to use at a given latitude. For more info on this problem see this thread or this one, or search the web for "convert meters to latitude longitude".
I have a database full of map Locations (Latitude, Longitude), is there a way I could do a SQL query to return me all points around a certain location?
You can easily check a square region.
Using very simplified latitude / longitude coordinates say you're at [25.86, 57.03] and you wanted everything in the "neighborhood" (+/- .05) you can use a query like this:
SELECT * FROM Coords WHERE (latitude BETWEEN 25.81 AND 25.91) AND (longitude BETWEEN 56.98 AND 57.08);
You can also use the SQLite R*Tree extension.
An R-Tree is a special index that is designed for doing range queries. R-Trees are most commonly used in geospatial systems where each entry is a rectangle with minimum and maximum X and Y coordinates.
The source code to the SQLite R*Tree module is included as part of the SQLite3 amalgamation but is disabled by default. To enable the R*Tree module, simply compile with the SQLITE_ENABLE_RTREE C-preprocessor macro defined.

Categories

Resources