{"id":354,"date":"2022-06-24T17:02:37","date_gmt":"2022-06-24T09:02:37","guid":{"rendered":"https:\/\/blog.osvlabs.com\/?p=354"},"modified":"2022-06-24T17:16:22","modified_gmt":"2022-06-24T09:16:22","slug":"php-%e5%9c%b0%e7%90%86%e5%9d%90%e6%a0%87%e5%ba%93mjaschen-phpgeo%e7%9a%84%e4%bb%8b%e7%bb%8d%ef%bc%88%e8%b7%9d%e7%a6%bb%e5%92%8c%e7%94%b5%e5%ad%90%e5%9b%b4%e6%a0%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.osvlabs.com\/?p=354","title":{"rendered":"PHP \u5730\u7406\u5750\u6807\u5e93mjaschen\/phpgeo\u7684\u4ecb\u7ecd\uff08\u8ddd\u79bb\u548c\u7535\u5b50\u56f4\u680f\uff09"},"content":{"rendered":"<h1>\u7b80\u4ecb<\/h1>\n<p><a href=\"https:\/\/phpgeo.marcusjaschen.de\/Introduction.html\" title=\"mjaschen\/phpgeo\">mjaschen\/phpgeo<\/a>\u662f\u4e00\u4e2a\u5c0f\u578bPHP\u5e93\uff0c\u63d0\u4f9b\u5730\u7406\u5750\u6807(\u5305\u62ec\u5bf9\u4e0d\u540c\u692d\u7403\u4f53\u7684\u652f\u6301)\u3001\u591a\u6298\u7ebf(GPS\u8f68\u8ff9)\u3001\u591a\u8fb9\u5f62\u3001\u8fb9\u754c\u7b49\u62bd\u8c61\u3002Phpgeo\u5141\u8bb8\u4f60\u7528\u8fd9\u4e9b\u62bd\u8c61\u6765\u6267\u884c\u4e0d\u540c\u7684\u8ba1\u7b97\uff0c\u6bd4\u5982\u8ddd\u79bb\uff0c\u8f68\u9053\u957f\u5ea6\u7b49\u3002\u672c\u7ae0\u6211\u4eec\u4e3b\u8981\u4ecb\u7ecd\u5b83\u5bf9\u8ba1\u7b97\u8ddd\u79bb\u548c\u7535\u5b50\u56f4\u680f\u7684\u4f7f\u7528\u3002<\/p>\n<h2>\u7248\u672c\u8981\u6c42<\/h2>\n<blockquote>\n<p>\u6839\u636e\u5176\u5b98\u7f51\u4f7f\u7528\u8bf4\u660e\uff1a<br \/>\nphpgeo requires at least PHP 7.3. phpgeo fully supports PHP 8.<br \/>\nThe 3.x releases require PHP &gt;= 7.2 but don\u2019t get feature updates any longer. Bugfixes will be backported.<br \/>\nThe 2.x releases require PHP &gt;= 7.0 but don\u2019t get feature updates any longer. Bugfixes won\u2019t be backported.<br \/>\nThe 1.x release line has support for PHP &gt;= 5.4. Bugfixes won\u2019t be backported<br \/>\n\u5176\u4ed6\u7684PHP\u7248\u672c\uff0c\u8bf7\u53c2\u8003<a href=\"https:\/\/packagist.org\/packages\/mjaschen\/phpgeo\" title=\"\u8fd9\u91cc\">packagist\u8fd9\u91cc<\/a>\u7684\u7248\u672c\u5bf9\u7167\uff0c\u627e\u5230\u5408\u9002\u7684\u7248\u672c<\/p>\n<\/blockquote>\n<h1>\u5b89\u88c5<\/h1>\n<pre><code>composer require mjaschen\/phpgeo<\/code><\/pre>\n<h1>\u4f7f\u7528<\/h1>\n<blockquote>\n<p>\u5728\u4ecb\u7ecd\u4f7f\u7528\u4e4b\u524d\uff0c\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\uff0cphpgeo\u9ed8\u8ba4\u4f7f\u7528<strong>wGS-84\u5750\u6807\u7cfb<\/strong>\uff08\u5982\u679c\u6709\u4e0d\u4e86\u89e3\u5750\u6807\u7cfb\u7684\u8bf7\u81ea\u884c\u767e\u5ea6\u4e86\u89e3\uff09\uff0c\u4e5f\u5c31\u662f\u56fd\u9645\u5750\u6807\u7cfb\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u4f7f\u7528\u7684\u8bbe\u5907\u901a\u8fc7GPS\u83b7\u53d6\u7684\u5750\u6807\u4f7f\u7528\u7684\u5c31\u662f\u8fd9\u4e2a\u5750\u6807\u7cfb\uff0c\u4f46\u5728\u5730\u56fe\u4e0a\u83b7\u53d6\u7684\u7ecf\u7eac\u5ea6\u5c31\u4e0d\u4e00\u5b9a\u662f\u8fd9\u4e2a\u5750\u6807\u7cfb\uff0c\u5c24\u5176\u662f\u56fd\u5185\u7684\u5730\u56fe\uff0c\u6240\u4ee5\u5728\u56fd\u5185\u4f7f\u7528\u5730\u56fe\u4e0a\u83b7\u53d6\u7684\u7ecf\u7eac\u5ea6\u8ba1\u7b97\u65f6\u9700\u8981\u8f6c\u6362\u4e00\u4e0b\u3002\u4e0b\u9762\u4f8b\u5b50\u90fd\u5c06\u9ed8\u8ba4\u4f7f\u7528<strong>wGS-84\u5750\u6807\u7cfb<\/strong>\u505a\u6f14\u793a\u3002\u5b98\u7f51\u7684\u539f\u6587\u662f\u8fd9\u6837\u8bf4\u7684\uff1aphpgeo supports arbitrary ellipsoids. WGS-84 is used as default when no other ellipsoid is given. For day-to-day calculations it\u2019s not needed to care about ellipsoids in the most cases<\/p>\n<\/blockquote>\n<h2>\u8ddd\u79bb\u8ba1\u7b97<\/h2>\n<p>\u5173\u4e8e\u8ddd\u79bb\u8ba1\u7b97\u5176\u5b9e\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u53bb\u8ba1\u7b97\uff0c\u7b2c\u4e00\u79cd\u662f\u901a\u8fc7\u4e24\u70b9\u751f\u6210\u4e00\u6761\u7ebf\uff0c\u7136\u540e\u8ba1\u7b97\u8fd9\u6761\u7ebf\u7684\u957f\u5ea6\uff0c\u53e6\u4e00\u79cd\u662f\u76f4\u63a5\u8ba1\u7b97\u4e24\u70b9\u7684\u8ddd\u79bb<\/p>\n<h3>\u7b2c\u4e00\u79cd\u901a\u8fc7\u751f\u6210\u4e00\u6761\u7ebf\u8ba1\u7b97\u957f\u5ea6<\/h3>\n<pre><code class=\"language-PHP\">&lt;?php\n\nuse Location\\Coordinate;\nuse Location\\Distance\\Haversine;\nuse Location\\Line;\n\n$line = new Line(\n    new Coordinate(52.5, 13.5),\n    new Coordinate(52.6, 13.4)\n);\n\/\/\u8fd9\u91cc\u8ba1\u7b97\u51fa\u7684\u957f\u5ea6\u5355\u4f4d\u662f\u7c73\n$length = $line-&gt;getLength(new Haversine());\n\nprint_r( &quot;\u8fd9\u6761\u7ebf\u7684\u957f\u5ea6\u4e3a&quot;.$length.&quot;\u7c73&quot;)\n\n\/\/\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6765\u8fd9\u6761\u7ebf\u7684\u5176\u4ed6\u4fe1\u606f\n\/\/\u4f8b\u5982 \u4e2d\u95f4\u70b9\n\n$midpoint = $line-&gt;getMidpoint();\n\nprintf(\n            &#039;\u8fd9\u6761\u7ebf\u7684\u4e2d\u95f4\u70b9\u7684\u7eac\u5ea6\u662f\uff1a %.3f \uff1b\u7ecf\u5ea6\u662f\uff1a %.3f .&#039;,\n           $midpoint-&gt;getLat(),\n           $midpoint-&gt;getLng(),\n        );\n<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u79cd\u76f4\u63a5\u8ba1\u7b97\u4e24\u70b9\u7684\u8ddd\u79bb<\/h3>\n<p>\u76f4\u63a5\u4f7f\u7528\u8ba1\u7b97\u5668\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-PHP\">&lt;?php\n\nuse Location\\Coordinate;\nuse Location\\Distance\\Vincenty;\n\n$coordinate1 = new Coordinate(19.820664, -155.468066);\n$coordinate2 = new Coordinate(20.709722, -156.253333);\n\n$calculator = new Vincenty();\n\necho $calculator-&gt;getDistance($coordinate1, $coordinate2);<\/code><\/pre>\n<p>\u6216\u8005\u901a\u8fc7\u6ce8\u5165\u8ba1\u7b97\u5668\u5b9e\u4f8b\u6765\u8c03\u7528 <code>Coordinate<\/code> \u5b9e\u4f8b\u7684 <code>getDistance()<\/code> \u65b9\u6cd5<\/p>\n<pre><code class=\"language-PHP\">&lt;?php\n\nuse Location\\Coordinate;\nuse Location\\Distance\\Vincenty;\n\n$coordinate1 = new Coordinate(19.820664, -155.468066);\n$coordinate2 = new Coordinate(20.709722, -156.253333);\n\necho $coordinate1-&gt;getDistance($coordinate2, new Vincenty());<\/code><\/pre>\n<h2>\u7535\u5b50\u56f4\u680f<\/h2>\n<blockquote>\n<p>\u6211\u4eec\u8fd9\u91cc\u8bf4\u7684\u7535\u5b50\u56f4\u680f\u5176\u5b9e\u662f\u6307\u5730\u56fe\u4e0a\u7684\u7535\u5b50\u56f4\u680f\uff0c\u4e5f\u5c31\u662f\u5728\u5730\u56fe\u4e0a\u753b\u51fa\u56f4\u680f\uff0c\u7136\u540e\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u8ba1\u7b97\u76ee\u6807\u548c\u8fd9\u4e2a\u56f4\u680f\u7684\u76f8\u5bf9\u5173\u7cfb<\/p>\n<\/blockquote>\n<p>Phpgeo\u6709\u4e00\u4e2a\u591a\u8fb9\u5f62\u5b9e\u73b0\uff0c\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u4e00\u4e2a\u51e0\u4f55\u56fe\u5f62(\u70b9\uff0c\u7ebf\uff0cpolyline\uff0c\u591a\u8fb9\u5f62)\u662f\u5426\u5305\u542b\u5728\u5176\u4e2d\u3002\u4e00\u4e2a\u591a\u8fb9\u5f62\u81f3\u5c11\u7531\u4e09\u4e2a\u70b9\u7ec4\u6210\u3002<\/p>\n<pre><code class=\"language-PHP\">&lt;?php\n\nuse Location\\Coordinate;\nuse Location\\Polygon;\n\n$geofence = new Polygon();\n\n$geofence-&gt;addPoint(new Coordinate(-12.085870,-77.016261));\n$geofence-&gt;addPoint(new Coordinate(-12.086373,-77.033813));\n$geofence-&gt;addPoint(new Coordinate(-12.102823,-77.030938));\n$geofence-&gt;addPoint(new Coordinate(-12.098669,-77.006476));\n\n$outsidePoint = new Coordinate(-12.075452, -76.985079);\n$insidePoint = new Coordinate(-12.092542, -77.021540);\n\necho $geofence-&gt;contains($outsidePoint)\n    ? &#039;Point 1 is located inside the polygon&#039; . PHP_EOL\n    : &#039;Point 1 is located outside the polygon&#039; . PHP_EOL;\n\necho $geofence-&gt;contains($insidePoint)\n    ? &#039;Point 2 is located inside the polygon&#039; . PHP_EOL\n    : &#039;Point 2 is located outside the polygon&#039; . PHP_EOL;<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\uff1a<\/p>\n<pre><code>Point 1 is located outside the polygon\nPoint 2 is located inside the polygon<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa\u76ee\u6807\u662f\u5426\u5728\u56f4\u680f\u4e2d\uff0c\u800c\u5e94\u7528\u5230\u5b9e\u9645\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c<strong>\u6211\u4eec\u901a\u5e38\u662f\u5728\u76ee\u6807\u8fdb\u5165\u56f4\u680f\u6216\u8005\u79fb\u51fa\u56f4\u680f\u65f6\u89e6\u53d1\u4e0b\u4e00\u6b65\u4e1a\u52a1\uff0c\u8fd9\u5c31\u8981\u7ed3\u5408\u6570\u636e\u5e93\uff0c\u5b58\u50a8\u76ee\u6807\u6bcf\u6b21\u4f4d\u7f6e\u53d1\u751f\u53d8\u5316\u540e\u76f8\u5bf9\u56f4\u680f\u7684\u5185\u5916\u5173\u7cfb\uff0c\u7136\u540e\u8fd9\u6b21\u53c8\u548c\u4e0a\u6b21\u7684\u5185\u5916\u5173\u7cfb\u505a\u6bd4\u5bf9\uff0c\u786e\u5b9a\u76ee\u6807\u548c\u56f4\u680f\u7684\u76f8\u5bf9\u5173\u7cfb\u662f\u5426\u53c8\u53d8\u5316\uff0c\u4ece\u800c\u5224\u65ad\u51fa\u76ee\u6807\u662f\u8fdb\u5165\u56f4\u680f\u8fd8\u662f\u79fb\u51fa\u56f4\u680f\uff0c\u518d\u6839\u636e\u5224\u65ad\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u4e1a\u52a1\u64cd\u4f5c<\/strong><\/p>\n<h4>\u7ed3\u8bed<\/h4>\n<p>mjaschen\/phpgeo \u662f\u4e00\u4e2a\u5f88\u597d\u7684PHP\u5e93\uff0c\u5e76\u4e14\u4ece\u76ee\u524d\u770b\uff0c\u4f5c\u8005\u7ef4\u62a4\u4e5f\u6bd4\u8f83\u597d\u3002\u4e0a\u9762\u53ea\u662f\u7b80\u5355\u4ecb\u7ecd\u4e86\u5b83\u7684\u4e24\u4e2a\u529f\u80fd\uff0c\u8fd8\u6709\u66f4\u591a\u7684\u529f\u80fd\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u53bb\u5b83\u7684\u6587\u6863\u4e0a\u5b66\u4e60\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5bf9\u4e8e\u7a7a\u95f4\u7684\u8ba1\u7b97\uff0c\u5b83\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c<strong>\u4f46\u662f<\/strong>\uff0c\u5b83\u5e76\u4e0d\u662f\u6700\u597d\u7684\u9009\u62e9\uff0c\u5982\u679c\u4f60\u5bf9PostgreSQL\u6709\u4e86\u89e3\uff0c\u5efa\u8bae\u4f7f\u7528\u5b83\u7ed3\u5408PostGIS\uff0c\u5728\u7a7a\u95f4\u8ba1\u7b97\u4e0a\uff0cPostGIS\u5728\u6027\u80fd\u548c\u4f7f\u7528\u9886\u57df\u6709\u66f4\u5e7f\u9614\u7684\u7528\u9014\u3002<\/p>\n<p>\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u8bf7\u70b9\u8d5e+\u8bc4\u8bba\uff0c\u5c31\u4e0b\u671f\u7ee7\u7eed\u5206\u4eab\u5173\u4e8e\u7a7a\u95f4\u8ba1\u7b97\u7684\u8fdb\u9636\u7bc7\uff1a<strong>PostGIS \u8ddd\u79bb\u8ba1\u7b97\u5efa\u8bae<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb mjaschen\/phpgeo\u662f\u4e00\u4e2a\u5c0f\u578bPHP\u5e93\uff0c\u63d0\u4f9b\u5730\u7406\u5750\u6807(\u5305\u62ec\u5bf9\u4e0d\u540c\u692d\u7403\u4f53\u7684\u652f\u6301)\u3001\u591a\u6298\u7ebf(GPS\u8f68\u8ff9)\u3001\u591a\u8fb9\u5f62\u3001\u8fb9\u754c\u7b49\u62bd\u8c61\u3002Phpgeo\u5141\u8bb8\u4f60\u7528\u8fd9\u4e9b\u62bd\u8c61\u6765\u6267\u884c\u4e0d\u540c\u7684\u8ba1\u7b97\uff0c\u6bd4\u5982\u8ddd\u79bb\uff0c\u8f68\u9053\u957f\u2026<\/p>\n","protected":false},"author":10,"featured_media":384,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"_links":{"self":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/354"}],"collection":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":28,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions\/382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/media\/384"}],"wp:attachment":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}