{"id":6966,"date":"2025-06-02T14:23:08","date_gmt":"2025-06-05T06:58:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6966"},"modified":"2025-06-05T14:23:08","modified_gmt":"2025-06-05T06:58:08","slug":"https-badgameshow-com-steven-159","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-159\/","title":{"rendered":"Swift \u5730\u5716\u529f\u80fd\u5b8c\u6574\u6559\u5b78\uff1a\u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u8207\u6b65\u884c\u8def\u7dda"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u5730\u5716, \u6a19\u8a18\u4f4d\u7f6e, \u9078\u64c7\u5730\u9ede, \u6b65\u884c\u8def\u7dda\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728iOS\u958b\u767c\u4e2d\uff0c\u5730\u5716\u529f\u80fd\u662f\u8a31\u591a\u61c9\u7528\u7a0b\u5f0f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u5be6\u73fe\u5730\u5716\u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u548c\u6b65\u884c\u8def\u7dda\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b2025\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u3002<\/p>\n<h2>\u5efa\u7acbSwift\u5c08\u6848\u8207\u5c0e\u5165MapKit<\/h2>\n<p>\u9996\u5148\uff0c\u5728Xcode\u4e2d\u5efa\u7acb\u4e00\u500b\u65b0\u7684Swift\u5c08\u6848\uff0c\u4e26\u78ba\u4fdd\u5c0e\u5165MapKit\u6846\u67b6\u3002MapKit\u6846\u67b6\u662fApple\u63d0\u4f9b\u7684\u5f37\u5927\u5de5\u5177\uff0c\u80fd\u8f15\u9b06\u5be6\u73fe\u5730\u5716\u529f\u80fd\u3002<\/p>\n<h2>\u8a2d\u7f6eMKMapView<\/h2>\n<p>\u5728\u5c08\u6848\u4e2d\u5efa\u7acb\u4e00\u500b\u65b0\u7684ViewController\uff0c\u4e26\u5c0e\u5165MapKit\u6846\u67b6\u3002\u7136\u5f8c\uff0c\u5ba3\u544a\u4e00\u500b<strong>MKMapView<\/strong>\u7684\u5be6\u4f8b\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70baViewController\u7684view\u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import MapKit\n\nclass ViewController: UIViewController {\n    let mapView = MKMapView()\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        view = mapView\n        setupMapView()\n    }\n\n    private func setupMapView() {\n        mapView.frame = view.bounds\n        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]\n    }\n}\n<\/code><\/pre>\n<h2>\u6a19\u8a18\u5730\u9ede<\/h2>\n<p>\u4f7f\u7528<strong>MKMapView<\/strong>\u7684<strong>addAnnotation<\/strong>\u65b9\u6cd5\u4f86\u6a19\u8a18\u7279\u5b9a\u4f4d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let annotation = MKPointAnnotation()\nannotation.coordinate = CLLocationCoordinate2D(latitude: 25.03, longitude: 121.6)\nannotation.title = \"Taipei 101\"\nmapView.addAnnotation(annotation)\n<\/code><\/pre>\n<h2>\u986f\u793a\u8207\u9078\u64c7\u6a19\u8a18<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528<strong>showAnnotations<\/strong>\u65b9\u6cd5\u4f86\u986f\u793a\u6a19\u8a18\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">mapView.showAnnotations([annotation], animated: true)\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u9078\u64c7\u7279\u5b9a\u7684\u6a19\u8a18\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>selectAnnotation<\/strong>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">mapView.selectAnnotation(annotation, animated: true)\n<\/code><\/pre>\n<h2>\u8a08\u7b97\u6b65\u884c\u8def\u7dda<\/h2>\n<p>\u4f7f\u7528<strong>MKDirections<\/strong>\u4f86\u8a08\u7b97\u6b65\u884c\u8def\u7dda\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let request = MKDirections.Request()\nrequest.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 25.03, longitude: 121.6)))\nrequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 25.04, longitude: 121.6)))\nrequest.transportType = .walking\n\nlet directions = MKDirections(request: request)\ndirections.calculate { response, error in\n    guard let response = response else { return }\n    for route in response.routes {\n        self.mapView.addOverlay(route.polyline)\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u8a08\u7b97\u5f9e\u53f0\u5317101\u5230\u53e6\u4e00\u500b\u5730\u9ede\u7684\u6b65\u884c\u8def\u7dda\uff0c\u4e26\u5728\u5730\u5716\u4e0a\u986f\u793a\u8def\u5f91\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\uff0c\u4f8b\u5982\u8def\u5f91\u8a08\u7b97\u5931\u6557\u3002\u5efa\u8b70\u5728\u8a08\u7b97\u8def\u5f91\u6642\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u8a8d\u8d77\u59cb\u9ede\u548c\u7d42\u9ede\u7684\u5750\u6a19\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u5df2\u7372\u5f97\u5fc5\u8981\u7684\u5730\u7406\u4f4d\u7f6e\u6b0a\u9650\u3002<\/li>\n<li>\u6aa2\u67e5\u7db2\u8def\u9023\u63a5\u662f\u5426\u6b63\u5e38\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u6a19\u8a18\u4f4d\u7f6e\u548c\u8a08\u7b97\u8def\u7dda\u5916\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u5982\u5730\u5716\u8986\u84cb\u5c64\u3001\u7528\u6236\u8ffd\u8e64\u7b49\u529f\u80fd\uff0c\u4ee5\u589e\u5f37\u61c9\u7528\u7a0b\u5f0f\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5730\u5716-\ud83d\uddfa\ufe0f-\u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u3001\u6b65\u884c\u8def\u7dda_1675399385.8985631.webp\" alt=\"Swift \u5730\u5716\u529f\u80fd\u793a\u7bc4\" title=\"Swift \u5730\u5716 \ud83d\uddfa\ufe0f \u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u3001\u6b65\u884c\u8def\u7dda\u793a\u7bc4\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>Q1: \u5982\u4f55\u5728Swift\u4e2d\u7372\u53d6\u4f7f\u7528\u8005\u7684\u7576\u524d\u4f4d\u7f6e\uff1f<\/h3>\n<p>A1: \u53ef\u4ee5\u4f7f\u7528CLLocationManager\u985e\u5225\u4f86\u7372\u53d6\u4f7f\u7528\u8005\u7684\u7576\u524d\u4f4d\u7f6e\uff0c\u4e26\u5728\u5730\u5716\u4e0a\u6a19\u8a18\u8a72\u4f4d\u7f6e\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u81ea\u5b9a\u7fa9\u5730\u5716\u6a19\u8a18\u7684\u5916\u89c0\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u901a\u904e\u5be6\u73feMKMapViewDelegate\u5354\u8b70\u548c\u4f7f\u7528mapView(_:viewFor:)\u65b9\u6cd5\u4f86\u81ea\u5b9a\u7fa9\u6a19\u8a18\u7684\u5916\u89c0\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8655\u7406\u5730\u5716\u4e0a\u7684\u8def\u5f91\u91cd\u758a\u554f\u984c\uff1f<\/h3>\n<p>A3: \u7576\u8def\u5f91\u91cd\u758a\u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u4e0d\u540c\u984f\u8272\u6216\u6a23\u5f0f\u7684polyline\u4f86\u5340\u5206\u4e0d\u540c\u7684\u8def\u7dda\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u5730\u5716\ud83d\uddfa\ufe0f\u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u3001\u6b65\u884c\u8def\u7dda\uff0c\u4e26\u4e86\u89e3Swift\u5730\u5716\ud83d\uddfa\ufe0f\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Swift\u5730\u5716\ud83d\uddfa\ufe0f\u4f86\u63d0\u9ad8\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":6965,"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-6966","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-\ud83d\uddfa\ufe0f-\u6a19\u8a18\u4f4d\u7f6e\u3001\u9078\u64c7\u5730\u9ede\u3001\u6b65\u884c\u8def\u7dda_1675399385.8985631.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Om","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6966","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=6966"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6966\/revisions"}],"predecessor-version":[{"id":13369,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6966\/revisions\/13369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6965"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}