问题
I have a database of Cities my application supports. This includes following:
- Name.
- Center coordinates.
Nearest cities my app support are pretty far away.
Algorithm I want to implement is:
- Get current location
- Run through center coordinates, if anything is in <(X)Km from the user, then user is in that city.
Is this algorithm OK or should I re-think it some other way? My app currently supports 3 cities, however I plan to escalate & support more in some time.
Unfortunately, my main audience is abroad public, so internet access is kind of limited, e.g. I would implement an algorithm to determine my city name from internet using standard mechanic, but I can't relay on it as a default algorithm.
回答1:
You can use the following logic to get the distance between two lat long
(This is for oracle)
DISTANCE = (NVL(Radius,0) * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) +
(COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) *
Cos(Nvl(Lon2,0) / Degtorad - Nvl(Lon1,0)/ Degtorad))))
WhereDEGTORAD = 57.29577951;
Radius= 6387.7;
May be, similar logic can be implemented via a raw Query in SQLite DB.
The distance can be calculated and ranked and sorted based on least distance.
来源:https://stackoverflow.com/questions/23989909/algorithm-for-determining-city-name-w-o-internet-access