{"id":1115119,"date":"2025-01-08T18:03:47","date_gmt":"2025-01-08T10:03:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115119.html"},"modified":"2025-01-08T18:03:49","modified_gmt":"2025-01-08T10:03:49","slug":"python%e5%a6%82%e4%bd%95%e7%94%b1%e7%bb%8f%e7%ba%ac%e5%ba%a6%e7%94%9f%e4%ba%a7%e7%9c%81%e5%b8%82%e5%8e%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115119.html","title":{"rendered":"python\u5982\u4f55\u7531\u7ecf\u7eac\u5ea6\u751f\u4ea7\u7701\u5e02\u53bf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080015\/4642cdaa-6153-4e81-b325-5734ebd64db4.webp\" alt=\"python\u5982\u4f55\u7531\u7ecf\u7eac\u5ea6\u751f\u4ea7\u7701\u5e02\u53bf\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7531\u7ecf\u7eac\u5ea6\u751f\u6210\u7701\u5e02\u53bf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u4ece\u7ecf\u7eac\u5ea6\u751f\u6210\u7701\u5e02\u53bf\u4fe1\u606f\u65f6\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\u3001\u4f7f\u7528\u5730\u7406\u6570\u636e\u5e93\u3001\u7ed3\u5408\u7b2c\u4e09\u65b9API<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\u662f\u6700\u4e3a\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u9006\u5730\u7406\u7f16\u7801\uff0c\u60a8\u53ef\u4ee5\u5c06\u5177\u4f53\u7684\u7ecf\u7eac\u5ea6\u5750\u6807\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u5730\u5740\u4fe1\u606f\uff0c\u5305\u62ec\u7701\u3001\u5e02\u3001\u53bf\u7b49\u8be6\u7ec6\u5730\u5740\u3002\u5e38\u89c1\u7684\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\u63d0\u4f9b\u8005\u5305\u62ecGoogle Maps API\u3001B<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>du Maps API\u548cOpenStreetMap\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Google Maps API<\/h4>\n<\/p>\n<p><p>Google Maps API\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u9006\u5730\u7406\u7f16\u7801\u3002\u4e3a\u4e86\u4f7f\u7528Google Maps API\uff0c\u60a8\u9700\u8981\u9996\u5148\u83b7\u53d6\u4e00\u4e2aAPI\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_location_from_latlng(lat, lng, api_key):<\/p>\n<p>    url = f&quot;https:\/\/maps.googleapis.com\/maps\/api\/geocode\/json?latlng={lat},{lng}&amp;key={api_key}&quot;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        data = response.json()<\/p>\n<p>        if &#39;results&#39; in data and len(data[&#39;results&#39;]) &gt; 0:<\/p>\n<p>            address_components = data[&#39;results&#39;][0][&#39;address_components&#39;]<\/p>\n<p>            for component in address_components:<\/p>\n<p>                if &#39;administrative_area_level_1&#39; in component[&#39;types&#39;]:<\/p>\n<p>                    province = component[&#39;long_name&#39;]<\/p>\n<p>                if &#39;administrative_area_level_2&#39; in component[&#39;types&#39;]:<\/p>\n<p>                    city = component[&#39;long_name&#39;]<\/p>\n<p>                if &#39;administrative_area_level_3&#39; in component[&#39;types&#39;]:<\/p>\n<p>                    county = component[&#39;long_name&#39;]<\/p>\n<p>            return province, city, county<\/p>\n<p>    return None, None, None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>api_key = &#39;YOUR_API_KEY&#39;<\/p>\n<p>latitude = 39.9042<\/p>\n<p>longitude = 116.4074<\/p>\n<p>province, city, county = get_location_from_latlng(latitude, longitude, api_key)<\/p>\n<p>print(f&quot;\u7701: {province}, \u5e02: {city}, \u53bf: {county}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Baidu Maps API<\/h4>\n<\/p>\n<p><p>Baidu Maps API\u662f\u53e6\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\uff0c\u7279\u522b\u662f\u5728\u4e2d\u56fd\u3002\u60a8\u540c\u6837\u9700\u8981\u4e00\u4e2aAPI\u5bc6\u94a5\u6765\u4f7f\u7528\u8fd9\u4e2a\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_location_from_latlng_baidu(lat, lng, api_key):<\/p>\n<p>    url = f&quot;http:\/\/api.map.baidu.com\/reverse_geocoding\/v3\/?ak={api_key}&amp;output=json&amp;coordtype=wgs84ll&amp;location={lat},{lng}&quot;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        data = response.json()<\/p>\n<p>        if &#39;result&#39; in data:<\/p>\n<p>            address_component = data[&#39;result&#39;][&#39;addressComponent&#39;]<\/p>\n<p>            province = address_component.get(&#39;province&#39;)<\/p>\n<p>            city = address_component.get(&#39;city&#39;)<\/p>\n<p>            county = address_component.get(&#39;district&#39;)<\/p>\n<p>            return province, city, county<\/p>\n<p>    return None, None, None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>api_key = &#39;YOUR_API_KEY&#39;<\/p>\n<p>latitude = 39.9042<\/p>\n<p>longitude = 116.4074<\/p>\n<p>province, city, county = get_location_from_latlng_baidu(latitude, longitude, api_key)<\/p>\n<p>print(f&quot;\u7701: {province}, \u5e02: {city}, \u53bf: {county}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5730\u7406\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8API\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5730\u7406\u6570\u636e\u5e93\u3002\u4f8b\u5982\uff0cGeoNames\u6570\u636e\u5e93\u5305\u542b\u4e86\u4e30\u5bcc\u7684\u5730\u7406\u4fe1\u606f\uff0c\u53ef\u4ee5\u7528\u4e8e\u9006\u5730\u7406\u7f16\u7801\u3002\u4e0d\u8fc7\uff0c\u8fd9\u9700\u8981\u60a8\u4e0b\u8f7d\u548c\u914d\u7f6e\u6570\u636e\u5e93\uff0c\u5e76\u7f16\u5199\u67e5\u8be2\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def get_location_from_latlng_db(lat, lng, db_path):<\/p>\n<p>    conn = sqlite3.connect(db_path)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    query = f&quot;&quot;&quot;<\/p>\n<p>    SELECT province, city, county FROM locations<\/p>\n<p>    WHERE lat BETWEEN {lat-0.01} AND {lat+0.01}<\/p>\n<p>    AND lng BETWEEN {lng-0.01} AND {lng+0.01}<\/p>\n<p>    LIMIT 1<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    result = cursor.fetchone()<\/p>\n<p>    conn.close()<\/p>\n<p>    if result:<\/p>\n<p>        return result[0], result[1], result[2]<\/p>\n<p>    return None, None, None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>db_path = &#39;geonames.db&#39;<\/p>\n<p>latitude = 39.9042<\/p>\n<p>longitude = 116.4074<\/p>\n<p>province, city, county = get_location_from_latlng_db(latitude, longitude, db_path)<\/p>\n<p>print(f&quot;\u7701: {province}, \u5e02: {city}, \u53bf: {county}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u7b2c\u4e09\u65b9API<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Google Maps\u548cBaidu Maps\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u63d0\u4f9b\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\u7684API\uff0c\u4f8b\u5982OpenStreetMap\u7684Nominatim\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenStreetMap Nominatim API<\/h4>\n<\/p>\n<p><p>OpenStreetMap\u7684Nominatim\u670d\u52a1\u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5730\u7406\u7f16\u7801\u548c\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_location_from_latlng_osm(lat, lng):<\/p>\n<p>    url = f&quot;https:\/\/nominatim.openstreetmap.org\/reverse?format=json&amp;lat={lat}&amp;lon={lng}&amp;zoom=10&amp;addressdetails=1&quot;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        data = response.json()<\/p>\n<p>        if &#39;address&#39; in data:<\/p>\n<p>            address = data[&#39;address&#39;]<\/p>\n<p>            province = address.get(&#39;state&#39;)<\/p>\n<p>            city = address.get(&#39;city&#39;)<\/p>\n<p>            county = address.get(&#39;county&#39;)<\/p>\n<p>            return province, city, county<\/p>\n<p>    return None, None, None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>latitude = 39.9042<\/p>\n<p>longitude = 116.4074<\/p>\n<p>province, city, county = get_location_from_latlng_osm(latitude, longitude)<\/p>\n<p>print(f&quot;\u7701: {province}, \u5e02: {city}, \u53bf: {county}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u4ece\u7ecf\u7eac\u5ea6\u83b7\u53d6\u7701\u5e02\u53bf\u4fe1\u606f\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u9006\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u5982Google Maps API\u548cBaidu Maps API\uff0c\u662f\u6700\u4e3a\u4fbf\u6377\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u4e0d\u5e0c\u671b\u4f9d\u8d56\u5916\u90e8API\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5730\u7406\u6570\u636e\u5e93\u7684\u65b9\u6cd5\uff0c\u800cOpenStreetMap\u7684Nominatim\u670d\u52a1\u5219\u63d0\u4f9b\u4e86\u4e00\u4e2a\u514d\u8d39\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u901a\u8fc7\u7ecf\u7eac\u5ea6\u83b7\u53d6\u7701\u5e02\u53bf\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u5982\u767e\u5ea6\u5730\u56feAPI\u6216\u9ad8\u5fb7\u5730\u56feAPI\uff0c\u5c06\u7ecf\u7eac\u5ea6\u8f93\u5165\u5230\u8fd9\u4e9b\u670d\u52a1\u4e2d\uff0c\u4fbf\u80fd\u8fd4\u56de\u5bf9\u5e94\u7684\u7701\u3001\u5e02\u3001\u53bf\u4fe1\u606f\u3002\u8fd9\u4e9b\u670d\u52a1\u901a\u5e38\u63d0\u4f9b\u7b80\u5355\u7684HTTP\u8bf7\u6c42\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u7684\u65b9\u5f0f\u5b9e\u73b0\u81ea\u52a8\u5316\u67e5\u8be2\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5b9e\u73b0\u7ecf\u7eac\u5ea6\u5230\u7701\u5e02\u53bf\u8f6c\u6362\u7684\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u5982<code>geopy<\/code>\u3001<code>geocoder<\/code>\u548c<code>reverse_geopy<\/code>\u7b49\u3002\u8fd9\u4e9b\u5e93\u901a\u8fc7\u8c03\u7528\u5730\u7406\u7f16\u7801API\u5b9e\u73b0\u7ecf\u7eac\u5ea6\u7684\u53cd\u5411\u67e5\u8be2\uff0c\u8fd4\u56de\u76f8\u5173\u5730\u7406\u4fe1\u606f\uff0c\u5305\u62ec\u7701\u5e02\u53bf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406API\u8c03\u7528\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u9650\u5236\u6216\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u5730\u56feAPI\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8bf7\u6c42\u6b21\u6570\u9650\u5236\u3001\u5730\u7406\u7f16\u7801\u5931\u8d25\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u7b49\u65b9\u6cd5\u6765\u4f18\u5316\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6548\u7387\u3002\u6b64\u5916\uff0c\u9009\u62e9\u5408\u9002\u7684API\u5957\u9910\u4e5f\u80fd\u6709\u6548\u907f\u514d\u9650\u6d41\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u7531\u7ecf\u7eac\u5ea6\u751f\u6210\u7701\u5e02\u53bf \u5728\u4f7f\u7528Python\u4ece\u7ecf\u7eac\u5ea6\u751f\u6210\u7701\u5e02\u53bf\u4fe1\u606f\u65f6\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u9006\u5730\u7406\u7f16 [&hellip;]","protected":false},"author":3,"featured_media":1115124,"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\/1115119"}],"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=1115119"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115119\/revisions"}],"predecessor-version":[{"id":1115125,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115119\/revisions\/1115125"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115124"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}