{"id":6820,"date":"2025-06-03T13:29:03","date_gmt":"2025-06-05T05:56:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6820"},"modified":"2025-06-05T13:29:03","modified_gmt":"2025-06-05T05:56:03","slug":"https-badgameshow-com-steven-86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-86\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u5730\u5716\u5c0e\u822a\u6280\u5de7\u8207\u5be6\u4f5c\u6307\u5357 \ud83d\uddfa\ufe0f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u5730\u5716\u5c0e\u822a, \u5730\u5716\u5c0e\u822a\u6280\u5de7, MapKit, iOS \u958b\u767c\"><\/p>\n<h1>2025 \u6700\u65b0 Swift \u5730\u5716\u5c0e\u822a\u6280\u5de7\u8207\u5be6\u4f5c\u6307\u5357 \ud83d\uddfa\ufe0f<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u958b\u767c\u5730\u5716\u5c0e\u822a\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u5206\u4eab\u4e00\u4e9b\u5be6\u7528\u7684\u5730\u5716\u5c0e\u822a\u6280\u5de7\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>\u8981\u958b\u767c\u5730\u5716\u5c0e\u822a\u61c9\u7528\u7a0b\u5f0f\uff0c\u60a8\u9700\u8981\u4f7f\u7528 Swift \u7684 <strong>MapKit<\/strong> \u6846\u67b6\u3002\u9019\u500b\u6846\u67b6\u5141\u8a31\u60a8\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528\u5730\u5716\u4e26\u63d0\u4f9b\u591a\u7a2e\u529f\u80fd\uff0c\u5305\u62ec\u5730\u5716\u6a19\u8a18\u3001\u8def\u7dda\u898f\u5283\u548c\u8def\u7dda\u5c0e\u822a\u3002<\/p>\n<h2>\u5982\u4f55\u958b\u59cb\u4f7f\u7528 MapKit \u6846\u67b6<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c07 MapKit \u6846\u67b6\u65b0\u589e\u5230\u60a8\u7684 Xcode \u5c08\u6848\u4e2d\u3002\u5728 Xcode \u7684\u300cTargets\u300d\u9078\u55ae\u4e2d\uff0c\u9078\u64c7\u60a8\u7684\u5c08\u6848\uff0c\u7136\u5f8c\u5728\u300cGeneral\u300d\u9078\u9805\u5361\u4e2d\uff0c\u5c07 MapKit \u6846\u67b6\u65b0\u589e\u5230\u300cLinked Frameworks and Libraries\u300d\u6e05\u55ae\u4e2d\u3002<\/p>\n<h2>\u986f\u793a\u5730\u5716\u8207\u8a08\u7b97\u8def\u7dda<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 MapKit \u6846\u67b6\u4f86\u958b\u767c\u5730\u5716\u5c0e\u822a\u61c9\u7528\u7a0b\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u8a72\u7bc4\u4f8b\u986f\u793a\u5730\u5716\u4e26\u8a08\u7b97\u5f9e\u7576\u524d\u4f4d\u7f6e\u5230\u76ee\u7684\u5730\u7684\u8def\u7dda\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import MapKit\n\n\/\/ \u5efa\u7acb MKMapView \u7269\u4ef6\nlet mapView = MKMapView()\n\n\/\/ \u8a2d\u5b9a\u76ee\u7684\u5730\nlet destination = CLLocationCoordinate2D(latitude: 37.785834, longitude: -122.406417)\n\n\/\/ \u8a08\u7b97\u8def\u7dda\nlet request = MKDirections.Request()\nrequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: destination))\n\nlet directions = MKDirections(request: request)\ndirections.calculate { response, error in\n    guard let response = response else {\n        \/\/ \u8655\u7406\u932f\u8aa4\n        print(\"Error calculating directions: \\(String(describing: error))\")\n        return\n    }\n\n    \/\/ \u5c07\u8def\u7dda\u6dfb\u52a0\u5230\u5730\u5716\n    mapView.addOverlay(response.routes[0].polyline)\n}\n<\/code><\/pre>\n<h2>\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u53ef\u7528\u6027<\/h2>\n<p>\u5728\u958b\u767c\u5730\u5716\u5c0e\u822a\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\u4f86\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <strong>MKMapSnapshotter<\/strong> \u985e\u5225\u4f86\u751f\u6210\u5730\u5716\u5feb\u7167\uff0c\u4ee5\u4fbf\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u986f\u793a\u5730\u5716\u3002<\/li>\n<li>\u4f7f\u7528 <strong>MKGeodesicPolyline<\/strong> \u985e\u5225\u4f86\u7e6a\u88fd\u8def\u7dda\uff0c\u9019\u53ef\u4ee5\u4f7f\u8def\u7dda\u7684\u8996\u89ba\u6548\u679c\u66f4\u52a0\u81ea\u7136\u3002<\/li>\n<li>\u5229\u7528 <strong>MKLocalSearch<\/strong> \u985e\u5225\u4f86\u641c\u5c0b\u9644\u8fd1\u7684\u5730\u9ede\uff0c\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<li>\u900f\u904e <strong>MKUserTrackingMode<\/strong> \u985e\u5225\u4f86\u8ffd\u8e64\u4f7f\u7528\u8005\u7684\u4f4d\u7f6e\uff0c\u63d0\u4f9b\u5373\u6642\u5c0e\u822a\u529f\u80fd\u3002<\/li>\n<li>\u4f7f\u7528 <strong>MKMapCamera<\/strong> \u985e\u5225\u4f86\u63a7\u5236\u5730\u5716\u7684\u8996\u89d2\uff0c\u4ee5\u4fbf\u70ba\u7528\u6236\u63d0\u4f9b\u6700\u4f73\u7684\u8996\u89ba\u6548\u679c\u3002<\/li>\n<\/ul>\n<h2>\u555f\u52d5\u7cfb\u7d71\u5730\u5716\u61c9\u7528\u7a0b\u5f0f<\/h2>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>MKMapItem<\/strong> \u985e\u5225\u4f86\u555f\u52d5\u7cfb\u7d71\u5730\u5716\u61c9\u7528\u7a0b\u5f0f\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u8f15\u9b06\u5c0e\u822a\u5230\u76ee\u7684\u5730\u3002\u9019\u5c0d\u65bc\u90a3\u4e9b\u559c\u6b61\u4f7f\u7528\u5167\u5efa\u5730\u5716\u61c9\u7528\u7a0b\u5f0f\u7684\u7528\u6236\u4f86\u8aaa\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u4f7f\u7528 Swift \u958b\u767c\u5730\u5716\u5c0e\u822a\u61c9\u7528\u7a0b\u5f0f\u662f\u4e00\u500b\u5f88\u6709\u8da3\u4e14\u5be6\u7528\u7684\u904e\u7a0b\uff0c\u80fd\u5920\u63d0\u5347\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u529f\u80fd\u4e26\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a18\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>MKPointAnnotation<\/strong> \u985e\u5225\u4f86\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a18\uff0c\u900f\u904e\u8a2d\u7f6e\u5176 <code>coordinate<\/code> \u548c <code>title<\/code> \u4f86\u986f\u793a\u7279\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406\u8def\u7dda\u8a08\u7b97\u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728\u8a08\u7b97\u8def\u7dda\u6642\uff0c\u60a8\u53ef\u4ee5\u5728\u56de\u8abf\u4e2d\u6aa2\u67e5 <code>error<\/code> \u53c3\u6578\uff0c\u4e26\u6839\u64da\u932f\u8aa4\u985e\u578b\u63d0\u4f9b\u76f8\u61c9\u7684\u63d0\u793a\u6216\u91cd\u8a66\u908f\u8f2f\u3002<\/p>\n<h3>3. \u5982\u4f55\u6539\u5584\u5730\u5716\u7684\u8f09\u5165\u901f\u5ea6\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u5730\u5716\u5feb\u7167\u529f\u80fd\u4f86\u9810\u52a0\u8f09\u5730\u5716\uff0c\u4e26\u5728\u9700\u8981\u6642\u986f\u793a\uff0c\u7528\u4ee5\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u53cd\u61c9\u901f\u5ea6\u8207\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5730\u5716\u5c0e\u822a-\ud83d\uddfa\ufe0f-\u5730\u5716\u5c0e\u822a\u6280\u5de7_1675396373.5835562.webp\" alt=\"Swift \u5730\u5716\u5c0e\u822a \ud83d\uddfa\ufe0f \u5730\u5716\u5c0e\u822a\u6280\u5de7\" title=\"Swift \u5730\u5716\u5c0e\u822a\u3001iOS \u5730\u5716\u5c0e\u822a\u3001Swift \u5c0e\u822a API\u3001\u5730\u5716\u5c0e\u822a\u529f\u80fd\u3001\u7528\u6236\u9ad4\u9a57\" width=\"1200\" height=\"628\"\/><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u5730\u5716\u5c0e\u822a\u6280\u5de7\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528\u5730\u5716\u5c0e\u822aAPI\u4f86\u958b\u767ciOS\u61c9\u7528\u7a0b\u5f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5730\u5716\u5c0e\u822a\u529f\u80fd\u4f86\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":6819,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5730\u5716\u5c0e\u822a-\ud83d\uddfa\ufe0f-\u5730\u5716\u5c0e\u822a\u6280\u5de7_1675396373.5835562.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1M0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6820"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6820\/revisions"}],"predecessor-version":[{"id":13514,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6820\/revisions\/13514"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6819"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}