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.
Related
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
I am using android fused location api to get the latitude and longitude of same position but every time i get different latitude and longitude of same position last 4 digits vary every time at same position why so and how to get all the possible latitude & longitude of same position.
So please help me regarding this.
Location is generally accurate to only a city block, and will change within that. Try rounding off the coordinates to two decimal places and use those.
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.
I have a data set of different locations, and want to show the nearest locations (within 5 km).
How can I determine the minimum/maximum of latitude and longitude?
f.e.: I need to fill my car up, and am looking for all gas stations in my neighborhood so I can go to the nearest. How do I do this on an Android phone?
I'd like to avoid iterating through all of the locations as well, because I've got about 2500 locations and rising. Any suggestions on that?
Thank you guys in advance for the advice on this!
Update:
Thank you for the feedback you guys gave me, I solved my issue by iterating through all locations on the server and using the Google Distance Matrix API to calculate the distances: http://code.google.com/intl/nl/apis/maps/documentation/distancematrix/
Simplifying, latitude is the angle over/under the equator, longitude is angle right/left of greenwich meridian.
So to calculate (on average) how much for example 1ยบ latitude is, you convert it to radians (multiply by PI/180), and then multiply by Earth's mean radius (6,371.0 km).
For your question, the process is the inverse one: take 5 km and convert it to degrees:
Divide it by Earth's radius
Multiply by 180/PI
This way you will get delta degrees, that is, how much degrees are 5 kms (on average, if you want exactitude, you will need the exact Earth radius differentials over those 5 kms) with which you can build a circle around the given location (just like a compass would).
All the calcuations give and methods are approximations but well within tolerances for what you require.
The earth circumference is approx 40076000 metres.
the distance traveled per degree of latitude is allways the same and is simply a proportion of the earth circ.
the distance travel per degree in longitude however changes depending upon your latitude ( this rings on the glode get smaller nearer the poles ).
so for a given distance m, the corresponing Latitude and Longitude values are
earthcirc = 40076000;
// at Lat and Lon for distance m (in meters)
LatDelta = (m * 360) / earthcirc;
LonDelta = (m * 360) / abs(eathcirc*cos(lat));
This gives you your square lat long deltas for a simple search of your data. but on fingin a candidate your should then do a full distance calc as the corner of the square is quite a bit more than 5 KM away.
distance between 2 lat/longs
distLat = (lat1-lat2) * earthcirc) / 360;
distLong = (long1-long2) * earthcirc * cos((lat1+lat2)/2) / 360;
dist = sqrt( sqr(distLat) + sqr(distLong) );
I know most compilers/languages use radians for cos/sin functions but its easire to explain in degrees.
as for searching your data the simplest way is to order in be either lat or long then you can do a binary search to find the possible location to check instead of a full scan. There are better ways to order the data ( quad trees ) but for 2500 ish entries i wouldnt bother
There are two issues here, 1) how to calculate the distance between two pairs of lat/lon and 2) how to find the point with shortest distance to a given point.
There are formulas on the net, some more accurate than others, for example http://www.movable-type.co.uk/scripts/latlong.html
This is a (geo) spatial indexing problem (http://en.wikipedia.org/wiki/Spatial_index#Spatial_Index ). You can use for example a quad tree with lat/lng as X/Y (I assume your points are not too close to the polars, which complicate things but still doable). The quad tree let you find in Log(N) time the neighborhood of your car without having to iterate over all points.
Not exact code but hopefully it will help.
I am new to Android. In my application, customer is at a location. I want to find agents near-by to that customer using his/her latitude and longitude. How can I do this? customer is on one location & We want to search agents surronding that perticular customer.
I have latitude of customer and agents and based on I want to search agents(From customer's latitude longitude in surrounding area of 5 km. which agents are thare that i want to search).
pseudocode
area = 100;
for a over allAgents
if(Math.abs(a.x - customer.x) < area || Math.abs(a.y - customer.y) < area)
nearCustomerArray.add(a);
If you already know agent's location, grab you current location using built-in device gps receiver.
Then, calculate distance between your coordinates and the one of the agent using distanceTo method of Location class
Finally, found the small distance among all the distances you calculated.