地理位置SQL查询未找到确切位置 [英] Geolocation SQL query not finding exact location

查看:137
本文介绍了地理位置SQL查询未找到确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在一直在测试我的地理位置查询,直到现在我还没有发现任何问题。



我试图搜寻给定范围内的所有城市,通常我会搜索城市周围的城市,但最近我尝试搜索周围的城市一个城市,发现这座城市本身并没有回归。



我将这些城市作为数据库中的摘录:

 城市纬度
Saint-Mathieu 45.316708 -73.516253
圣爱达德45.233374 -73.516254
圣米歇尔45.233374 -73.566256
圣雷米45.266708 -73.616257

但是当我在Saint-Rémi周围运行我的查询时,用以下查询...

  SELECT tblcity.city,tblcity.latitude,tblcity.longitude,
truncate((degrees(acos(sin(radians(tblcity.latitude))
* sin(弧度(45.266708))
+ cos(弧度(tblcity.latitude))
* cos(弧度(45.266708))
* cos(弧度(tblcity.longitude - -73.616257))))
* 69.09 * 1.6),1)作为距离
FROM tblcity HAVING distance< 10 ORDER BY distance desc

我得到以下结果:

 城市纬度经度距离
圣马修45.316708 -73.516253 9.5
圣爱达尔45.233374 -73.516254 8.6
圣米歇尔45.233374 -73.566256 5.3

圣雷米镇在搜索中不见了。



所以我尝试了一个修改后的查询,希望得到更好的结果:

  SELECT tblcity.city,tblcity.latitude,tblcity.longitude,
truncate((6371 * acos(cos(弧度(45.266708))
* cos(弧度(tblcity.latitude))$ b $ (弧度(tblcity.longitude))
- 弧度(-73.616257))
+ sin(弧度(45.266708))
* sin(弧度(tblcity.latitude)))), 1)AS距离
FROM tblcity HAVING距离< 10 ORDER BY距离desc

但是我得到了相同的结果...



然而,如果我通过将lat或long的最后一位数字改为1稍微修改Saint-Rémi的坐标系,那么这两个查询都会返回Saint-Rémi。另外,如果我将查询集中在上述任何其他城市中,搜索到的城市将返回结果中。

任何人都可以对可能导致我上面的查询的内容有所了解,以便不显示搜索到的Saint-Rémi城市?我在下面添加了一个表格样本(删除了额外的字段)。

我在使用MySQL 5.0.45,在此先感谢。

  CREATE TABLE`tblcity`(
`IDCity` int(1)NOT NULL auto_increment,
` City`varchar(155)NOT NULL默认值'',
`Latitude` decimal(9,6)NOT NULL默认值'0.000000',
`十进制经度'(9,6)NOT NULL默认值'0.000000 ',
PRIMARY KEY(`IDCity`)
)ENGINE = MyISAM AUTO_INCREMENT = 52743 DEFAULT CHARSET = latin1 AUTO_INCREMENT = 52743;

插入`tblcity`(`city`,`latitude`,`longitude`)值
('Saint-Mathieu',45.316708,-73.516253),
(' (圣米歇尔,45.233374,-73.566256),
('Saint-Rémi',45.266708,-73.616257);


解决方案

在您的第一个查询中,减法中的经度。 Cosine的球形定律是:
$ b $ pre code> d = acos(sin(lat1)* sin(lat2)+ cos(lat1)* cos(lat2)* cos(long2-long1))* R

如果lat1被替换为tblcity。纬度,long1必须用tblcity.longitude来代替。我想你已经在你的查询中意外地替换了long2。这个工作是否更好?

  SELECT tblcity.city,tblcity.latitude,tblcity.longitude,
truncate((弧度(45.266708))
+ cos(弧度(tblcity.latitude))
* cos(弧度(tblcity.latitude))
* sin(弧度(45.266708) )
* cos(弧度(-73.616257 -tblcity.longitude))))
* 69.09 * 1.6),1)作为距离
FROM tblcity HAVING distance< 10 ORDER BY distance desc

我还没有看过你的第二个查询,但希望这有帮助。

I have been testing my geolocation query for some time now and I haven't found any issues with it until now.

I am trying to search for all cities within a given radius, often times I'm searching for cities surrounding a city using that city's coords, but recently I tried searching around a city and found that the city itself was not returned.

I have these cities as an excerpt in my database:

city            latitude    longitude  
Saint-Mathieu   45.316708   -73.516253  
Saint-Édouard   45.233374   -73.516254  
Saint-Michel    45.233374   -73.566256  
Saint-Rémi      45.266708   -73.616257  

But when I run my query around the city of Saint-Rémi, with the following query...

SELECT tblcity.city, tblcity.latitude, tblcity.longitude, 
truncate((degrees(acos( sin(radians(tblcity.latitude)) 
* sin(radians(45.266708)) 
+ cos(radians(tblcity.latitude)) 
* cos(radians(45.266708)) 
* cos(radians(tblcity.longitude - -73.616257) ) ) ) 
* 69.09*1.6),1) as distance 
FROM tblcity HAVING distance < 10 ORDER BY distance desc 

I get these results:

city            latitude    longitude     distance  
Saint-Mathieu   45.316708   -73.516253    9.5  
Saint-Édouard   45.233374   -73.516254    8.6  
Saint-Michel    45.233374   -73.566256    5.3  

The town of Saint-Rémi is missing from the search.

So I tried a modified query hoping to get a better result:

SELECT tblcity.city, tblcity.latitude, tblcity.longitude, 
truncate(( 6371 * acos( cos( radians( 45.266708 ) ) 
* cos( radians( tblcity.latitude ) ) 
* cos( radians( tblcity.longitude ) 
- radians( -73.616257 ) ) 
+ sin( radians( 45.266708 ) ) 
* sin( radians( tblcity.latitude ) ) ) ),1) AS distance 
FROM tblcity HAVING distance < 10 ORDER BY distance desc 

But I get the same result...

However, if I modify Saint-Rémi's coords slighly by changing the last digit of the lat or long by 1, both queries will return Saint-Rémi. Also, if I center the query on any of the other cities above, the searched city is returned in the results.

Can anyone shed some light on what may be causing my queries above to not display the searched city of Saint-Rémi? I have added a sample of the table (with extra fields removed) below.

I'm using MySQL 5.0.45, thanks in advance.

CREATE TABLE `tblcity` ( 
`IDCity` int(1) NOT NULL auto_increment, 
`City` varchar(155) NOT NULL default '', 
`Latitude` decimal(9,6) NOT NULL default '0.000000', 
`Longitude` decimal(9,6) NOT NULL default '0.000000', 
PRIMARY KEY (`IDCity`) 
) ENGINE=MyISAM AUTO_INCREMENT=52743 DEFAULT CHARSET=latin1 AUTO_INCREMENT=52743; 

INSERT INTO `tblcity` (`city`, `latitude`, `longitude`) VALUES 
('Saint-Mathieu', 45.316708, -73.516253), 
('Saint-Édouard', 45.233374, -73.516254), 
('Saint-Michel', 45.233374, -73.566256), 
('Saint-Rémi', 45.266708, -73.616257); 

解决方案

In your first query, I believe you've inverted the longitudes in the subtraction. The Spherical Law of Cosines is:

d = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2−long1))*R

If lat1 is substituted with tblcity.latitude, long1 must be substituted with tblcity.longitude. I think you've accidentally substituted long2 in your query. Does this one work better?

SELECT tblcity.city, tblcity.latitude, tblcity.longitude, 
truncate((degrees(acos( sin(radians(tblcity.latitude)) 
* sin(radians(45.266708)) 
+ cos(radians(tblcity.latitude)) 
* cos(radians(45.266708)) 
* cos(radians(-73.616257 - tblcity.longitude) ) ) ) 
* 69.09*1.6),1) as distance 
FROM tblcity HAVING distance < 10 ORDER BY distance desc 

I haven't looked into your second query yet, but hopefully that helps.

这篇关于地理位置SQL查询未找到确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆