Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-如何获得具有多个纬度和经度的最近位置?_Mysql_Location_Gis - Fatal编程技术网

MySQL-如何获得具有多个纬度和经度的最近位置?

MySQL-如何获得具有多个纬度和经度的最近位置?,mysql,location,gis,Mysql,Location,Gis,我找到了一些很好的建议,用一对经纬度来寻找最近的位置。但我正在寻找一种有效的方法,用一组(多个)纬度和经度对找到最近的位置。然后将结果分组。我可以将mysql中的UNION与下面编写的查询一起使用。但我正在寻找一种没有工会的方式。我的表格(用户位置) 单lat和lng对查询:查找[48.205546,16.368667]附近的用户 SELECT name, ( 3959 * acos( cos( radians(48.205546) ) * cos( radians( locati

我找到了一些很好的建议,用一对经纬度来寻找最近的位置。但我正在寻找一种有效的方法,用一组(多个)纬度和经度对找到最近的位置。然后将结果分组。我可以将mysql中的UNION与下面编写的查询一起使用。但我正在寻找一种没有工会的方式。我的表格(用户位置)

单lat和lng对查询:查找[48.205546,16.368667]附近的用户

SELECT 
  name, 
   ( 3959 * acos( cos( radians(48.205546) ) * cos( radians( locations.lat ) ) 
   * cos( radians(locations.lng) - radians(16.368667)) + sin(radians(48.205546)) 
   * sin( radians(locations.lat)))) AS distance 
FROM user_locations 
WHERE active = 1 
HAVING distance < 10 
ORDER BY distance;

收集:[[48.205546,16.368667],[48.205084,16.369712],[48.205660,16.367947]]

SELECT 
      name, 
       ( 3959 * acos( cos( radians($lat1,$lat2,$lat3,$lat4) ) * cos( radians( locations.lat ) ) 
       * cos( radians(locations.lng) - radians($lng1,$lng2,$lng3,$lng4)) + sin(radians($lat)) 
       * sin( radians(locations.lat)))) AS distance 
    FROM user_locations 
    WHERE active = 1 
    HAVING distance < 10 
    ORDER BY distance;
黑客解决方案:

SELECT  locations.*  FROM    user_locations AS locations
WHERE   (MBRContains
                (
                LineString
                        (
                        Point (
                                16.368667 + 10 / ( 111.320 / COS(RADIANS(48.205546))),
                                48.205546 + 10 / 111.133
                              ),
                        Point (
                                16.368667 - 10 / ( 111.320 / COS(RADIANS(48.205546))),
                                48.205546 - 10 / 111.133
                              ) 
                        ),
                locations.point
                )
                OR MBRContains
                (
                LineString
                        (
                        Point (
                                16.369712 + 10 / ( 111.320 / COS(RADIANS(48.205084))),
                                48.205084 + 10 / 111.133
                              ),
                        Point (
                                16.369712 - 10 / ( 111.320 / COS(RADIANS(48.205084))),
                                48.205084 - 10 / 111.133
                              ) 
                        ),
                locations.point
                )
                      OR MBRContains
                (
                LineString
                        (
                        Point (
                                16.367947 + 10 / ( 111.320 / COS(RADIANS(48.205660))),
                                48.205660 + 10 / 111.133
                              ),
                        Point (
                                16.367947 - 10 / ( 111.320 / COS(RADIANS(48.205660))),
                                48.205660 - 10 / 111.133
                              ) 
                        ),
                locations.point
                ))
                
                
                      
                
                
        
查询持续时间:0.109秒(精细)。
注意:点字段具有空间索引

10(公里以内的距离)

16.368667(长)


48.205546(纬度)

什么是“具有多个纬度和经度的最近纬度长”?举几个例子。显示完整的点值,并说明该样本数据需要什么输出?请参阅@ysth修复问题并提供更多信息details@StrawberrydoneI只能再次向您提及我之前的评论
    +----+--------+-----------+-----------+-----------------------------+--+
| id | userid | lat       | lng       | point                       |  |
+----+--------+-----------+-----------+-----------------------------+--+
| 2  | 21     | 48.205872 | 16.368946 | POINT(16.368946, 48.205872) |  |
+----+--------+-----------+-----------+-----------------------------+--+
| 3  | 11     | 48.205914 | 16.367867 | POINT(16.367867, 48.205914) |  |
+----+--------+-----------+-----------+-----------------------------+--+
SELECT  locations.*  FROM    user_locations AS locations
WHERE   (MBRContains
                (
                LineString
                        (
                        Point (
                                16.368667 + 10 / ( 111.320 / COS(RADIANS(48.205546))),
                                48.205546 + 10 / 111.133
                              ),
                        Point (
                                16.368667 - 10 / ( 111.320 / COS(RADIANS(48.205546))),
                                48.205546 - 10 / 111.133
                              ) 
                        ),
                locations.point
                )
                OR MBRContains
                (
                LineString
                        (
                        Point (
                                16.369712 + 10 / ( 111.320 / COS(RADIANS(48.205084))),
                                48.205084 + 10 / 111.133
                              ),
                        Point (
                                16.369712 - 10 / ( 111.320 / COS(RADIANS(48.205084))),
                                48.205084 - 10 / 111.133
                              ) 
                        ),
                locations.point
                )
                      OR MBRContains
                (
                LineString
                        (
                        Point (
                                16.367947 + 10 / ( 111.320 / COS(RADIANS(48.205660))),
                                48.205660 + 10 / 111.133
                              ),
                        Point (
                                16.367947 - 10 / ( 111.320 / COS(RADIANS(48.205660))),
                                48.205660 - 10 / 111.133
                              ) 
                        ),
                locations.point
                ))