dpjs2005 2012-02-05 11:54
浏览 46
已采纳

地理编码 - 获取位置

If I have a table with

id, location, latitude, longitude

and I would have a location code: (13.779242,100.472915) for example.

! have 2 questions: what field type should I use for the lat and lon? Double? or maybe the spacial things? but isn't that more for calculating the distance?

Anyway, I want to find the location (or the one most accurate) Am I thinking to easily if I would just do these 2 queries:

Select * from location_table WHERE latitude > 13.779242 AND longitude > 100.472915." LIMIT 1"; // return as row 1
Select * from location_table WHERE latitude < 13.779242 AND longitude < 100.472915." LIMIT 1"; // return as row2

Then do a compare

$lat_dis_1 = $row1->latitude - 13.779242;
$lat_dis_2 = 13.779242 - $row2->latitude;

if ($lat_dis_1 > $lat_dis_2) {
// row2 was closer
} else {
//row1 closer
}

and same for longitude... Or am i just thinking too simpel here?

  • 写回答

2条回答 默认 最新

  • dongsong73032 2012-02-05 12:21
    关注

    The first thing you have to consider is that distance increases in a circle and not a square, meaning that just comparing longitude and latitude separately can especially in case of longer distances lead to false results. Here is a formula that explains the basic idea how you can relatively ieasily calcualte the true distance between two points: http://www.purplemath.com/modules/distform.htm.

    An additional point is when you cross the date line, where 2 points can be geographically a few km appart, but using just this method would appear half a world away.

    Yet another question is also what kind of performance you expect from this in comparison to accuracy. It would be a lot more efficient to use a simple square comparison if the accuracy is not that important, but speed is. Yet another possibility would be to combine the two techniques.

    A sample of the first technique would be (didn't test :) ):

    SELECT SQRT(POW(latitude-[INPUT LATITUDE], 2)+POW(longitude-[INPUT LONGITUDE])) as dist, * FROM location_table ORDER BY dist ASC LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?