简体   繁体   中英

sql query for map , lat , long distances giving illogical distance result

I'm trying to get the distances from a map point with a query from my database. I've used this to get distance on kilometers.

  SELECT title_fr,
         type,
         property_type,
         price,
         longi,
         lat, 
         ( 6371 * acos ( cos ( radians(10.811812877) ) * cos( radians( lat ) ) * cos( radians( longi ) - radians(36.461330195) ) + sin ( radians(10.811812877) ) * sin( radians( lat ) ) ) ) AS distance 
    FROM skopeo_annonce_immo 
ORDER BY distance

with my variables are :

latitude = 10.811812877
longitude = 36.461330195

my problem is that the query is giving false calculated results of distances. Example it is giving the distance of 3841.9933722712412 as distance result instead of 0 when latitude and longitude in my database are the same as the one used as arguments in my query . the other results are incoherents , they are giving me too very large distances such those.

title_fr                      type       property_type price      longi          lat             distance 
Villa avec picsine très...    to_sell    home          640000     11.035560071   33.825791858    3637.6770884050457
Belle maison à vendre         to_sell    home          192600     10.8492136     33.866210798    3653.92943440657
villa meublée a louer         to_rent    furnished     3000       10.70034027    34.774895801    3728.0785739669286
terrain à reougeb             to_sell    land          70         9.0293884277   33.449776583    3760.4640127561815
Appartement La Plage          to_rent    home          300        10.811812877   36.461330195    3841.9933722712412
Terrain Adel                  to_sell    land          270        10.809098482   36.462822475    3842.2851686112595
Appartement Maamoura          to_sell    apartment     180000     10.801491737   36.466369224    3843.056144544567
Dar  Maamoura Club            to_sell    home          400000     10.801513195   36.466403736    3843.057245114819
DAR L'ELEGANTE                to_sell    home          645000     10.82118988    36.485348924    3843.1331942217366

distances are in the last column . I've tried with other formulas , It's is giving me wrong results too .

Update and solution

Sometimes the solution is simple , the equation to get the distance that I have used is correct . The problem the fact that I have inverted the variables ! the correct order should be this .

latitude = 36.461330195
longitude = 10.811812877

and then it is good .

I would imagine the problem is probably with your equation not being correct given you coordinate projections? Which projection are you using? (I believe Google Maps is Spherical Mercator).

An alternative solution if you can't get the query working would be to use the maps api built in distance calculator:

var distance = google.maps.geometry.spherical.computeDistanceBetween(latLong, latLong);

You could easily do this inside a foreach loop on the client side to give you the distances you need.

For reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM