{"id":1017,"date":"2014-08-23T12:33:25","date_gmt":"2014-08-23T04:33:25","guid":{"rendered":"http:\/\/coderbee.net\/?p=1017"},"modified":"2014-08-23T12:33:25","modified_gmt":"2014-08-23T04:33:25","slug":"%e8%ae%a1%e7%ae%97-%e4%b8%80%e4%b8%aa%e7%82%b9-%e9%99%84%e8%bf%91%e7%9a%84%e5%9c%b0%e6%96%b9","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/web\/20140823\/1017","title":{"rendered":"\u8ba1\u7b97 \u4e00\u4e2a\u70b9 \u9644\u8fd1\u7684\u5730\u65b9"},"content":{"rendered":"<h3>\u95ee\u9898<\/h3>\n<p>\u968f\u7740\u5730\u7406\u4f4d\u7f6e\u7684\u5e94\u7528\u666e\u53ca\uff0c\u8d8a\u6765\u8d8a\u591a\u7c7b\u4f3c\u8ba1\u7b97 \u4e00\u4e2a\u70b9 \u9644\u8fd1\u7684\u9152\u5e97\u7684\u9700\u6c42\u3002\u6bd4\u5982\uff0c\u663e\u793a\u5f53\u524d\u4f4d\u7f6e2000\u7c73\u8303\u56f4\u5185\u7684 7 \u5929\u9152\u5e97\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u5728\u7cfb\u7edf\u91cc\u90fd\u6709\u4e00\u5f20\u8868\uff0c\u5b58\u50a8\u4e86\u6bcf\u4e2a 7 \u5929\u9152\u5e97\u7684\u4f4d\u7f6e\u4fe1\u606f\uff0c\u8868\u7ed3\u6784\u53ef\u4ee5\u7b80\u5316\u4e3a\u4e09\u4e2a\u5b57\u6bb5  <code>tb_location\uff08ID, lng, lat\uff09<\/code>\uff0c\u5176\u4e2d lng\uff1a\u8868\u793a\u7ecf\u5ea6\uff1blat\uff1a\u8868\u793a\u7eac\u5ea6\uff0c\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u90fd\u662f\u6570\u503c\u7c7b\u578b\u7684\uff0c\u4e14\u5efa\u4e86\u7d22\u5f15\u3002<\/p>\n<p>\u73b0\u5728\u8981\u628a 2 \u516c\u91cc\u8303\u56f4\u5185\u7684 7 \u5929\u9152\u5e97\u627e\u51fa\u6765\uff0c\u6709\u7684\u4eba\u5199\u7684 SQL \u8bed\u53e5\u5927\u6982\u662f\u8fd9\u6837\u7684\uff1a<br \/>\n<code>select id  from tb_location  where  calc_distance(lng,  lat,  curLng,  curLat) &lt; 2000<\/code><\/p>\n<p>\u8fd9\u4e2a\u8bed\u53e5\u7684\u95ee\u9898\u662f\u6ca1\u6cd5\u5229\u7528\u7d22\u5f15\uff0c\u9700\u8981\u5168\u8868\u626b\u63cf\uff0c\u8ba1\u7b97\u6bcf\u4e00\u6761\u8bb0\u5f55\u4e0e\u5f53\u524d\u4f4d\u7f6e\u7684\u8ddd\u79bb\uff0c\u6548\u7387\u5f88\u4f4e\u3002<\/p>\n<h3>\u4f18\u5316<\/h3>\n<p>\u7528\u4e00\u5f20\u56fe\u6765\u8bf4\u660e\u4f18\u5316\u7b56\u7565\uff1a<br \/>\n<img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2014\/08\/\u9644\u8fd1\u5730\u70b9\u8ba1\u7b97\u4f18\u5316.png\" alt=\"\u9644\u8fd1\u5730\u70b9\u8ba1\u7b97\u4f18\u5316\" \/><\/p>\n<p><!--more--><\/p>\n<p>2 \u516c\u91cc\u8303\u56f4\u662f\u4e00\u4e2a\u5706\uff0c\u8fd9\u4e2a\u5706\u7684\u8fb9\u754c\u662f\u7531\u4efb\u610f\u591a\u7684\u70b9\u786e\u5b9a\u7684\u3002\u4f46\u4e00\u4e2a\u6b63\u65b9\u5f62\u7684\u8fb9\u754c\u5374\u662f\u7531\u56db\u4e2a\u70b9\u786e\u5b9a\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u901a\u8fc7\u628a\u8981\u67e5\u627e\u7684\u5730\u70b9\u9650\u5b9a\u5728\u5305\u56f4\u8fd9\u4e2a\u5706\u7684\u6700\u5c0f\u6b63\u65b9\u5f62\u6765\u7f29\u5c0f\u67e5\u627e\u8303\u56f4\u3002<\/p>\n<p>\u8ba1\u7b97\u4ee3\u7801\u5982\u4e0b\uff08\u6536\u96c6\u81ea\u7f51\u7edc\uff09\uff1a<\/p>\n<pre><code class=\"java\">package net.coderbee.gps;\n\npublic class GPS {\n\n     private final double EARTH_RADIUS = 6378.137; \/\/ \u5730\u7403\u534a\u5f84\n\n     private static double rad(double d) {\n          return d * Math.PI \/ 180.0; \/\/ \u8ba1\u7b97\u5f27\u957f\n     }\n\n     \/**\n     * \u8ba1\u7b97\u4e24\u4e2a\u5730\u7406\u5750\u6807\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\n     * \n     * @param Lat1\n     * @param Lng1\n     * @param Lat2\n     * @param Lng2\n     * @return \u8ddd\u79bb\uff0c\u5355\u4f4d\u662f\u7c73\n     *\/\n     public double calcDistance(double Lat1, double Lng1, double Lat2,\n               double Lng2) {\n          double radLat1 = rad(Lat1);\n          double radLat2 = rad(Lat2);\n          double a = radLat1 - radLat2;\n          double b = rad(Lng1) - rad(Lng2);\n          double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a \/ 2), 2)\n                    + Math.cos(radLat1) * Math.cos(radLat2)\n                    * Math.pow(Math.sin(b \/ 2), 2)));\n          s = s * EARTH_RADIUS;\n          s = s * 1000; \/\/ \u6362\u7b97\u6210\u7c73\n          return s;\n\n     }\n\n     \/**\n     * \u8ba1\u7b97\u6700\u5c0f\u6b63\u65b9\u5f62\u7684\u56db\u4e2a\u7ecf\u7eac\u5ea6\u503c\n     * \n     * @param lng\n     *            \u7ecf\u5ea6\n     * @param lat\n     *            \u7eac\u5ea6\n     * @param distance\n     *            \u8ddd\u79bb\n     * @return\n     *\/\n     public static double[] getRectangle(double lng, double lat, long distance) {\n          float delta = 111000;\n\n          double minLng = lng - distance\n                    \/ Math.abs(Math.cos(Math.toRadians(lat)) * delta);\n          double maxLng = lng + distance\n                    \/ Math.abs(Math.cos(Math.toRadians(lat)) * delta);\n          double minLat = lat - (distance \/ delta);\n          double maxLat = lat + (distance \/ delta);\n          return new double[] { minLng, minLat, maxLng, maxLat };\n     }\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u53ef\u4ee5\u8f6c\u6362\u4e3a SQL \u7684\u51fd\u6570\u3002<\/p>\n<p>\u90a3\u4e48\u67e5\u8be2\u7684 SQL \u8bed\u53e5\u53ef\u4ee5\u4f18\u5316\u4e3a\uff1a<\/p>\n<pre><code class=\"sql\">select id from (\n     select loc.id, calc_distance(loc.lng,  loc.lat,  :curLng,  :curLat) distance\n     from tb_location loc \n     where loc.lng &gt;= :minLng and loc.lng &lt; :maxLng \n          and loc.lat &gt;= :minLat and loc.lat &lt; :maxLat\n) t \nwhere t.distance &lt; 2000\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898 \u968f\u7740\u5730\u7406\u4f4d\u7f6e\u7684\u5e94\u7528\u666e\u53ca\uff0c\u8d8a\u6765\u8d8a\u591a\u7c7b\u4f3c\u8ba1\u7b97 \u4e00\u4e2a\u70b9 \u9644\u8fd1\u7684\u9152\u5e97\u7684\u9700\u6c42\u3002\u6bd4\u5982\uff0c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/web\/20140823\/1017\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[231,186,187],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1017"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1017"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1017\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}