duanbinren8906 2011-08-18 18:58
浏览 349
已采纳

搜索范围纬度/经度坐标

This script works, but I am sure there is a better way to approach this. I have already tried storing them using the MySQL gis functionality any other suggestions would be helpful.

$vicinity = .001;

$lat_ceiling = floatval($lat) + floatval($vicinity);
$lat_floor =  floatval($lat) - floatval($vicinity);
$lng_ceiling = floatval($lng) + floatval($vicinity);
$lng_floor =  floatval($lng) - floatval($vicinity);

$query = "SELECT * FROM `geolocations` WHERE".
            "`latitude` <= '".$lat_ceiling."' AND `latitude` >= '". $lat_floor ."' AND `longitude` <= '".$lng_ceiling."' AND `longitude` >= '". $lng_floor ."'  "; 
  • 写回答

4条回答 默认 最新

  • duanjiu1950 2011-08-18 21:17
    关注

    Some version of what you are doing is right but I think you probably want to use the actual geometry types, especially if you are on MyISAM and can create an R-Tree spatial index. You can have columns with any supported type (ie point, polygon), or the catchall geometry type:

    mysql> create table spatial_table (id int, my_spots point, my_polygons geometry);
    Query OK, 0 rows affected (0.06 sec)
    

    Then query and update with WKT syntax:

    mysql> insert into spatial_table values (1, GeomFromText('POINT(1 1)'), GeomFromText('POLYGON((1 1, 2 2, 0 2, 1 1))'));
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into spatial_table values (1, GeomFromText('POINT(10 10)'), GeomFromText('POLYGON((10 10, 20 20, 0 20, 10 10))') );
    Query OK, 1 row affected (0.00 sec)
    

    You can then do your query (ie vicinity), against the minimum bounding rectangle of a linestring with terminating points point1 = longitude - increment, lon - increment, y = longitude + increment, latitude + increment, ie here with a +- of 1:

    mysql> select * from spatial_table where MBRContains(GeomFromText('LINESTRING(9 9, 11 11)'), my_spots);
    +------+---------------------------+-----------------------------------------------------------------------------------+
    | id   | my_spots                  | my_polygons                                                                       |
    +------+---------------------------+-----------------------------------------------------------------------------------+
    |    1 |              $@      $@ |                    $@      $@      4@      4@              4@      $@      $@ |
    +------+---------------------------+-----------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    This will perform much better than doing arithmetic on a bunch of floats representing longitude and latitude. BTW at about the location of San Francisco the following constants work pretty well for converting between km and degrees longitude and latitude (ie, if you want clean square maps of Santa Cruz):

    lonf 0.01132221938
    latf 0.0090215040
    

    That is, (x +- 2*lonf, y +- 2*latf) gives you the relevant $lat_floor etc values for a 2km wide boy around your point of interest.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装