简体   繁体   English

sql查询地图,纬度,长距离给出不合逻辑的距离结果

[英]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 . 例如,当数据库中的纬度和经度与查询中用作参数的纬度和经度相同时,给出的距离为3841.9933722712412 ,而不是0。 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). (我相信Google Maps是Spherical Mercator)。

An alternative solution if you can't get the query working would be to use the maps api built in distance calculator: 如果您无法使查询正常工作,另一种解决方案是使用距离计算器内置的maps api:

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. 您可以轻松地在客户端的foreach循环中执行此操作,以提供所需的距离。

For reference. 以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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