问题
I want to make an application in android. In this app user have to register in this app. With the registration the gps location(lat and long) will store in database table with user info. In this app i want to add a functionality that is "Search People who are in Root". User can input starting point and ending point(like : Mumbai to Delhi). And every peoples between this two point want to be found.
For Example : User-A have newly registered in this app. User-A's gps location is like lat = 20.234554 and long = 56.21455255. Now he will input two point like mumbai to delhi and click search button. Now i want to query in the database where every peoples information has been already stored. I want to query in database that "Select * from Location(Table) where peoples between mumbai to delhi"
Database table "locations" has four field id, lat, long and cityname. Now i want the above query on the database where i can find the people those are between the two inputted geo points.
Hope i have explained well that you understand what i want to achieve is only that i want to just search which are the nearby locations(lat and long) from the database table with inputted location(lat and long).
回答1:
I would suggest to limit your location by some conditions which can be indexed. You can do that easily as you probably don't want to list somebody from the US to somebody in Paris as nearest location.
With just a bit modification on RustamIS query you can eliminate the most of the irrelevant locations.
select * from gps_location_table t
where
t.x between 20.134554 and 20.334554 and t.y between 56.11455255 and 56.31455255
order by
sqrt((t.x - 20.234554)*(t.x - 20.234554) + (t.y - 56.21455255)*(t.y - 56.21455255)
desc
So you defined a rectangle where you want to look for the nearest people.
回答2:
SELECT *, ((ACOS(SIN(23.037698 * PI() / 180) * SIN(latitude * PI() / 180) + COS(23.037698 * PI() / 180) * COS(latitude * PI() / 180) * COS((72.564835 - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) as distance FROM location;
Above query will return you the distance if you enter
lat = 23.037698
long = 72.564835
Now compare distance say distance > 50kms.
This has worked for me.
回答3:
let long => Y and lat => X then query like this
select * from gps_location_table t
order by
sqrt(pow((t.x - 20.234554),2) + pow((t.y - 56.21455255),2))
desc
answer must be something like this!!! Mention that this formula does not return correct distance, but it works, and works faster than calculating real distance.
the main meaning is that the distance between two cordinates is:
sqrt((x1-x2)^2 + (y1-y2)^2)
来源:https://stackoverflow.com/questions/13136746/how-can-i-query-in-mysql-tablewhere-lat-and-long-stored-that-which-are-the-nea