I am trying to select elements inside a certain distance both in php with mySQL to notify their number and in Swift with distance: this is the mysql script:
SELECT ( 3959 * acos ( cos ( radians(38.760285006027) ) * cos( radians( graffiti.latitude ) ) * cos( radians( graffiti.longitude ) - radians(16.193975111137693) ) + sin ( radians(38.760285006027) ) * sin( radians( graffiti.latitude ) ) ) ) AS distance,
graffiti.message, graffiti.latitude, graffiti.longitude
FROM graffiti, users
WHERE users.Index=graffiti.user
AND (isPrivate=0 OR graffiti.user=users.Index)
HAVING distance < 0.03
producing:
0.003497087453447056 Pizzo Marinella è una ridente località della Calabria 38.760331000 16.193948000
0.028715511604188417 ❤️ 38.760047000 16.194412000
0.023340191076349676 Sono da Elena???? 38.760067000 16.194306000
0.025408688783029022 23 10 19 Ma si può avere ancora sole e caldo così ,dal roseto questo è quanto ????missá che pomeriggio vado al mare ciao Fa 38.760084000 16.194370000
0.02175324089254727 Quanto è bella Zara???? ???????????????????? 38.759993000 16.194126000
0.006165899762388741 Fa proprio caldo 38.760196000 16.193967000
0.000656926009429815 Io a casa 38.760294000 16.193979000
Where the first number is the distance, followed by the message and the coordinates. The same operation on Swift by code with location being: latitude 38.760285006027 and longitude 16.193975111137693:
for element in repository{
let distance=location.distance(from: element.location)
print("message \(element.message) distance \(distance) latitude \(element.latitude) longitude \(element.longitude)")
}
for instance returns, for the ❤️ message:
❤️ distance 46.260398663650726 latitude 38.760047 longitude 16.194412
Basically 46 meters in swift and 28 in mysql. and for the: Sono da Elena???? distance 37.58741285162493 latitude 38.760067 longitude 16.194306 37 meters in Swift and 23 meters in mysql.
Trusting Swift, what might be wrong in the mysql query for the discrepancy? Might it be that distance in Swift, differently of what it says in the documentation, also considers the relative altitudes?
The “3959” is used to calculate the distance in miles. You’d use “6371” for calculating the distance in kilometers.