普通网友 2013-04-08 17:55
浏览 77
已采纳

获取落在特定区域内的坐标

I'm making an API call which should return a list of points within certain area.

This will be used on a map, so the call to API has coordinates of all four corner points.
The area will always be a rectangle and will not be rotated.

At the moment, I'm using the following algorithm for limiting the results:

$lat_limits = array(min($bottom_left_lat, $top_left_lat), max($bottom_left_lat, $top_left_lat));
$lon_limits = array(min($top_left_lon, $top_right_lon),   max($top_left_lon, $top_right_lon));

My questions are:

  1. Is this an optimal way of searching?
  2. Are there any PHP libraries/helper functions that are better suited for this use?

Thanks!

  • 写回答

1条回答 默认 最新

  • dongyongyu0789 2013-04-08 18:17
    关注

    Simply:

    WHERE lat BETWEEN $lat_min AND $lat_max AND lng BETWEEN $lng_min AND $lng_max
    

    and put indexes on your lat and lng columns to better optimize the query.

    Just bear in mind that if left_lng > right_lng your window straddles the date line and you need to split it into 2 boxes at +/-180 degrees.

    Longitude generally increases from west to east with the exception of crossing the international date line where longitude changes from 180 degrees to -180 degrees. If you use the same query in a box that straddles this line you will get results for all longitudes outside the box. You need to detect the condition and split the query conditions into 2 boxes from lng_left to 180 and -180 to lng_right.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)