{"id":1121044,"date":"2025-01-08T19:04:53","date_gmt":"2025-01-08T11:04:53","guid":{"rendered":""},"modified":"2025-01-08T19:04:56","modified_gmt":"2025-01-08T11:04:56","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%a8%e7%bb%8f%e7%ba%ac%e5%ba%a6%e7%ae%97%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121044.html","title":{"rendered":"\u5982\u4f55\u7528python\u7528\u7ecf\u7eac\u5ea6\u7b97\u8ddd\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083439\/c6415e44-7f2b-49fb-9c8a-00acde651532.webp\" alt=\"\u5982\u4f55\u7528python\u7528\u7ecf\u7eac\u5ea6\u7b97\u8ddd\u79bb\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u7528\u7ecf\u7eac\u5ea6\u7b97\u8ddd\u79bb<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u8ba1\u7b97\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982Haversine\u516c\u5f0f\u3001Vincenty\u516c\u5f0f\u3001Geopy\u5e93\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u5e2e\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u5e94\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecdHaversine\u516c\u5f0f\u3001Geopy\u5e93\u3001Vincenty\u516c\u5f0f\u7b49\u65b9\u6cd5\uff0c\u5e76\u63a8\u8350\u4f7f\u7528Geopy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u4fbf\u4e14\u51c6\u786e\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001HAVERSINE\u516c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>Haversine\u516c\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u7684\u6570\u5b66\u516c\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u7403\u9762\uff08\u5982\u5730\u7403\uff09\u3002\u8be5\u516c\u5f0f\u53ef\u4ee5\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\uff0c\u5373\u5927\u5706\u8ddd\u79bb\u3002Haversine\u516c\u5f0f\u7684\u57fa\u672c\u539f\u7406\u662f\u901a\u8fc7\u7403\u9762\u4e09\u89d2\u5b66\u8ba1\u7b97\u4e24\u70b9\u7684\u5f27\u957f\u3002<\/p>\n<\/p>\n<p><h3>\u516c\u5f0f\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>[ a = \\sin^2\\left(\\frac{\\Delta\\phi}{2}\\right) + \\cos(\\phi_1) \\cdot \\cos(\\phi_2) \\cdot \\sin^2\\left(\\frac{\\Delta\\lambda}{2}\\right) ]<\/p>\n<p>[ c = 2 \\cdot \\text{atan2}\\left(\\sqrt{a}, \\sqrt{1-a}\\right) ]<\/p>\n<p>[ d = R \\cdot c ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li>(\\phi_1, \\phi_2) \u662f\u4e24\u70b9\u7684\u7eac\u5ea6\uff0c\u5355\u4f4d\u4e3a\u5f27\u5ea6\u3002<\/li>\n<li>(\\lambda_1, \\lambda_2) \u662f\u4e24\u70b9\u7684\u7ecf\u5ea6\uff0c\u5355\u4f4d\u4e3a\u5f27\u5ea6\u3002<\/li>\n<li>(\\Delta\\phi = \\phi_2 &#8211; \\phi_1)<\/li>\n<li>(\\Delta\\lambda = \\lambda_2 &#8211; \\lambda_1)<\/li>\n<li>(R) \u662f\u5730\u7403\u534a\u5f84\uff0c\u901a\u5e38\u53d6\u5e73\u5747\u503c6371\u516c\u91cc\u3002<\/li>\n<\/ul>\n<p><h3>Python\u5b9e\u73b0<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def haversine(lon1, lat1, lon2, lat2):<\/p>\n<p>    # \u5c06\u5341\u8fdb\u5236\u5ea6\u6570\u8f6c\u4e3a\u5f27\u5ea6<\/p>\n<p>    lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])<\/p>\n<p>    # Haversine\u516c\u5f0f<\/p>\n<p>    dlon = lon2 - lon1 <\/p>\n<p>    dlat = lat2 - lat1 <\/p>\n<p>    a = math.sin(dlat\/2)&lt;strong&gt;2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon\/2)&lt;\/strong&gt;2<\/p>\n<p>    c = 2 * math.asin(math.sqrt(a)) <\/p>\n<p>    # \u5730\u7403\u534a\u5f84\uff08\u516c\u91cc\uff09<\/p>\n<p>    r = 6371<\/p>\n<p>    return c * r<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>lon1, lat1 = -73.985428, 40.748817  # \u7ebd\u7ea6<\/p>\n<p>lon2, lat2 = -0.127758, 51.507351   # \u4f26\u6566<\/p>\n<p>distance = haversine(lon1, lat1, lon2, lat2)<\/p>\n<p>print(f&quot;\u8ddd\u79bb: {distance} \u516c\u91cc&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001GEOPY\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Geopy\u662f\u4e00\u4e2aPython\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5730\u7406\u8ba1\u7b97\uff0c\u5305\u62ec\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u8be5\u5e93\u5185\u7f6e\u4e86\u591a\u79cd\u7b97\u6cd5\uff0c\u4f7f\u7528\u975e\u5e38\u7b80\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Geopy<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Geopy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install geopy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Geopy\u8ba1\u7b97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>Geopy\u5e93\u63d0\u4f9b\u4e86<code>distance<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.distance import geodesic<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u70b9\u7684\u7ecf\u7eac\u5ea6<\/strong><\/h2>\n<p>ny = (40.748817, -73.985428)  # \u7ebd\u7ea6<\/p>\n<p>london = (51.507351, -0.127758)  # \u4f26\u6566<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = geodesic(ny, london).kilometers<\/p>\n<p>print(f&quot;\u8ddd\u79bb: {distance} \u516c\u91cc&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>Geopy\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>Geopy\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7b80\u4fbf\u6027\u548c\u591a\u529f\u80fd\u6027\u3002\u9664\u4e86\u8ba1\u7b97\u5730\u7403\u4e0a\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u5916\uff0cGeopy\u8fd8\u53ef\u4ee5\u5904\u7406\u5730\u5740\u89e3\u6790\u3001\u5730\u7406\u7f16\u7801\u7b49\u590d\u6742\u7684\u5730\u7406\u8ba1\u7b97\u4efb\u52a1\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u9891\u7e41\u8fdb\u884c\u5730\u7406\u8ba1\u7b97\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001VINCENTY\u516c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>Vincenty\u516c\u5f0f\u662f\u4e00\u79cd\u66f4\u7cbe\u786e\u7684\u8ba1\u7b97\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u692d\u7403\u4f53\u3002\u5b83\u8003\u8651\u4e86\u5730\u7403\u7684\u692d\u7403\u5f62\u72b6\uff0c\u56e0\u6b64\u5728\u8ba1\u7b97\u957f\u8ddd\u79bb\u65f6\u6bd4Haversine\u516c\u5f0f\u66f4\u52a0\u51c6\u786e\u3002\u7136\u800c\uff0cVincenty\u516c\u5f0f\u7684\u8ba1\u7b97\u8fc7\u7a0b\u76f8\u5bf9\u590d\u6742\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u8ba1\u7b97\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>Vincenty\u516c\u5f0f\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684Geopy\u5e93\u4e5f\u63d0\u4f9b\u4e86Vincenty\u516c\u5f0f\u7684\u5b9e\u73b0\uff0c\u53ef\u4ee5\u901a\u8fc7<code>vincenty<\/code>\u51fd\u6570\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.distance import vincenty<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u70b9\u7684\u7ecf\u7eac\u5ea6<\/strong><\/h2>\n<p>ny = (40.748817, -73.985428)  # \u7ebd\u7ea6<\/p>\n<p>london = (51.507351, -0.127758)  # \u4f26\u6566<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = vincenty(ny, london).kilometers<\/p>\n<p>print(f&quot;\u8ddd\u79bb: {distance} \u516c\u91cc&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>vincenty<\/code>\u51fd\u6570\u5728Geopy\u7684\u8f83\u65b0\u7248\u672c\u4e2d\u5df2\u7ecf\u88ab\u5f03\u7528\uff0c\u5efa\u8bae\u4f7f\u7528<code>geodesic<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u5728\u5185\u90e8\u4e5f\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Vincenty\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5927\u91cf\u7684\u6570\u636e\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u5305\u542b\u7ecf\u7eac\u5ea6\u5bf9\u7684\u6570\u636e\u96c6\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8ba1\u7b97\u6bcf\u5bf9\u6570\u636e\u7684\u8ddd\u79bb\u5462\uff1f\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Pandas<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u5305\u542b\u591a\u5bf9\u7ecf\u7eac\u5ea6\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u8bfb\u53d6\u6587\u4ef6\u5e76\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from geopy.distance import geodesic<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;coordinates.csv&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u8ba1\u7b97\u8ddd\u79bb\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def calculate_distance(row):<\/p>\n<p>    point1 = (row[&#39;lat1&#39;], row[&#39;lon1&#39;])<\/p>\n<p>    point2 = (row[&#39;lat2&#39;], row[&#39;lon2&#39;])<\/p>\n<p>    return geodesic(point1, point2).kilometers<\/p>\n<h2><strong>\u5e94\u7528\u51fd\u6570\u5230\u6bcf\u4e00\u884c<\/strong><\/h2>\n<p>data[&#39;distance&#39;] = data.apply(calculate_distance, axis=1)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u6bd4\u8f83\u548c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u5bf9\u51e0\u79cd\u5e38\u7528\u7b97\u6cd5\u7684\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><h3>Haversine\u516c\u5f0f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u61c2\uff0c\u8ba1\u7b97\u901f\u5ea6\u5feb\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u8ba1\u7b97\u957f\u8ddd\u79bb\u65f6\u7cbe\u5ea6\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><h3>Vincenty\u516c\u5f0f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7cbe\u5ea6\u9ad8\uff0c\u7279\u522b\u9002\u7528\u4e8e\u957f\u8ddd\u79bb\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8ba1\u7b97\u8fc7\u7a0b\u590d\u6742\uff0c\u901f\u5ea6\u8f83\u6162\u3002<\/li>\n<\/ul>\n<p><h3>Geopy\u5e93<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u591a\u529f\u80fd\uff0c\u6613\u4e8e\u4f7f\u7528\u3002<\/li>\n<li>\u5185\u7f6e\u591a\u79cd\u7b97\u6cd5\u53ef\u9009\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002<\/li>\n<\/ul>\n<p><h3>\u9009\u62e9\u5efa\u8bae<\/h3>\n<\/p>\n<p><p><strong>\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528Geopy\u5e93\u7684geodesic\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u4fbf\u4e14\u51c6\u786e\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Vincenty\u516c\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/strong><\/p>\n<\/p>\n<p><h3>\u6848\u4f8b\u4e00\uff1a\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u8ba1\u7b97\u4e16\u754c\u5404\u5927\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528Geopy\u5e93\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cities = {<\/p>\n<p>    &#39;New York&#39;: (40.748817, -73.985428),<\/p>\n<p>    &#39;London&#39;: (51.507351, -0.127758),<\/p>\n<p>    &#39;Paris&#39;: (48.856613, 2.352222),<\/p>\n<p>    &#39;Tokyo&#39;: (35.689487, 139.691711),<\/p>\n<p>}<\/p>\n<p>for city1, coord1 in cities.items():<\/p>\n<p>    for city2, coord2 in cities.items():<\/p>\n<p>        if city1 != city2:<\/p>\n<p>            distance = geodesic(coord1, coord2).kilometers<\/p>\n<p>            print(f&quot;\u8ddd\u79bb: {city1} \u5230 {city2}: {distance:.2f} \u516c\u91cc&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6848\u4f8b\u4e8c\uff1a\u7269\u6d41\u8def\u5f84\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u7269\u6d41\u884c\u4e1a\uff0c\u8def\u5f84\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u5047\u8bbe\u6211\u4eec\u6709\u591a\u4e2a\u914d\u9001\u70b9\uff0c\u9700\u8981\u8ba1\u7b97\u6bcf\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u4ee5\u4f18\u5316\u914d\u9001\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">delivery_points = [<\/p>\n<p>    (40.748817, -73.985428),  # \u7ebd\u7ea6<\/p>\n<p>    (51.507351, -0.127758),   # \u4f26\u6566<\/p>\n<p>    (48.856613, 2.352222),    # \u5df4\u9ece<\/p>\n<p>    (35.689487, 139.691711)   # \u4e1c\u4eac<\/p>\n<p>]<\/p>\n<h2><strong>\u8ba1\u7b97\u6240\u6709\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u77e9\u9635<\/strong><\/h2>\n<p>distance_matrix = [[geodesic(p1, p2).kilometers for p2 in delivery_points] for p1 in delivery_points]<\/p>\n<h2><strong>\u8f93\u51fa\u8ddd\u79bb\u77e9\u9635<\/strong><\/h2>\n<p>for row in distance_matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6848\u4f8b\u4e09\uff1a\u5730\u7406\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\uff0c\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97\u4e5f\u5f88\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Folium\u5e93\u5c06\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import folium<\/p>\n<h2><strong>\u521b\u5efa\u5730\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>m = folium.Map(location=[20, 0], zoom_start=2)<\/p>\n<h2><strong>\u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0<\/strong><\/h2>\n<p>cities = {<\/p>\n<p>    &#39;New York&#39;: (40.748817, -73.985428),<\/p>\n<p>    &#39;London&#39;: (51.507351, -0.127758),<\/p>\n<p>    &#39;Paris&#39;: (48.856613, 2.352222),<\/p>\n<p>    &#39;Tokyo&#39;: (35.689487, 139.691711),<\/p>\n<p>}<\/p>\n<p>for city, coord in cities.items():<\/p>\n<p>    folium.Marker(coord, popup=city).add_to(m)<\/p>\n<h2><strong>\u6dfb\u52a0\u57ce\u5e02\u4e4b\u95f4\u7684\u8fde\u7ebf<\/strong><\/h2>\n<p>for city1, coord1 in cities.items():<\/p>\n<p>    for city2, coord2 in cities.items():<\/p>\n<p>        if city1 != city2:<\/p>\n<p>            folium.PolyLine([coord1, coord2], color=&quot;blue&quot;, weight=2.5, opacity=1).add_to(m)<\/p>\n<h2><strong>\u4fdd\u5b58\u5730\u56fe<\/strong><\/h2>\n<p>m.save(&#39;map.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6848\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u8ba1\u7b97\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5e7f\u6cdb\u7684\u7528\u9014\u3002\u65e0\u8bba\u662f\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97\u3001\u7269\u6d41\u8def\u5f84\u4f18\u5316\uff0c\u8fd8\u662f\u5730\u7406\u53ef\u89c6\u5316\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e9b\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8ba1\u7b97\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u6ee1\u8db3\u7b80\u5355\u7684\u8ba1\u7b97\u9700\u6c42\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u590d\u6742\u7684\u5b9e\u9645\u573a\u666f\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Haversine\u516c\u5f0f\u3001Geopy\u5e93\u3001Vincenty\u516c\u5f0f\u7b49\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u5e2e\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u5e94\u7528\u3002<strong>\u63a8\u8350\u4f7f\u7528Geopy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u4fbf\u4e14\u51c6\u786e\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u662f\u8fdb\u884c\u5730\u7406\u6570\u636e\u5206\u6790\u3001\u8def\u5f84\u4f18\u5316\uff0c\u8fd8\u662f\u5730\u7406\u53ef\u89c6\u5316\uff0c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u5c06\u5bf9\u4f60\u7684\u5de5\u4f5c\u5927\u6709\u88e8\u76ca\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u4f60\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\u548c\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e24\u4e2a\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8ba1\u7b97\u4e24\u4e2a\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528Haversine\u516c\u5f0f\u3002\u8fd9\u4e2a\u516c\u5f0f\u8003\u8651\u4e86\u5730\u7403\u7684\u66f2\u7387\uff0c\u9002\u5408\u8ba1\u7b97\u5730\u7403\u8868\u9762\u4e24\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8ddd\u79bb\u3002\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e2a\u516c\u5f0f\uff0c\u6216\u8005\u76f4\u63a5\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>geopy<\/code>\u6765\u7b80\u5316\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u8ba1\u7b97\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7ecf\u7eac\u5ea6\u8ddd\u79bb\u8ba1\u7b97\u3002<code>geopy<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u53ea\u9700\u5b89\u88c5\u8fd9\u4e2a\u5e93\uff0c\u5e76\u4f7f\u7528\u5176\u5185\u7f6e\u7684\u8ddd\u79bb\u8ba1\u7b97\u529f\u80fd\uff0c\u5c31\u80fd\u5feb\u901f\u5f97\u5230\u7ed3\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528Haversine\u516c\u5f0f\u8ba1\u7b97\u8ddd\u79bb\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Haversine\u516c\u5f0f\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684\u7ecf\u7eac\u5ea6\u503c\u662f\u4ee5\u5f27\u5ea6\u800c\u975e\u5ea6\u4e3a\u5355\u4f4d\u3002\u6b64\u5916\uff0c\u8ba1\u7b97\u7ed3\u679c\u901a\u5e38\u662f\u4ee5\u516c\u91cc\u4e3a\u5355\u4f4d\uff0c\u82e5\u9700\u8981\u5176\u4ed6\u5355\u4f4d\uff08\u5982\u82f1\u91cc\uff09\uff0c\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u8f6c\u6362\u3002\u786e\u4fdd\u7ecf\u7eac\u5ea6\u7684\u987a\u5e8f\u6b63\u786e\uff08\u7eac\u5ea6\u5728\u524d\uff0c\u7ecf\u5ea6\u5728\u540e\uff09\uff0c\u4ee5\u907f\u514d\u8ba1\u7b97\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u7528\u7ecf\u7eac\u5ea6\u7b97\u8ddd\u79bb \u4f7f\u7528Python\u8ba1\u7b97\u7ecf\u7eac\u5ea6\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982Haversin [&hellip;]","protected":false},"author":3,"featured_media":1121051,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121044"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1121044"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121044\/revisions"}],"predecessor-version":[{"id":1121053,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121044\/revisions\/1121053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}