{"id":423,"date":"2025-05-29T10:33:53","date_gmt":"2025-06-05T03:30:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=423"},"modified":"2025-06-05T10:33:53","modified_gmt":"2025-06-05T03:30:53","slug":"swift-%e5%9c%b0%e5%9c%96-%e8%87%aa%e5%b7%b1%e5%ba%a7%e6%a8%99-%e5%a4%a7%e9%a0%ad%e9%87%9d","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e5%9c%b0%e5%9c%96-%e8%87%aa%e5%b7%b1%e5%ba%a7%e6%a8%99-%e5%a4%a7%e9%a0%ad%e9%87%9d\/","title":{"rendered":"Swift \u5730\u5716\u61c9\u7528\uff1a\u63d2\u5165\u5927\u982d\u91dd\u8207\u67e5\u8a62\u7576\u524d\u5ea7\u6a19\u7684\u5b8c\u6574\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p>&#8220;`markdown<\/p>\n<h1>Swift \u5730\u5716\u61c9\u7528\uff1a\u63d2\u5165\u5927\u982d\u91dd\u8207\u67e5\u8a62\u7576\u524d\u5ea7\u6a19\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<h2>Swift\u5730\u5716\u6982\u8ff0<\/h2>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u4f7f\u7528\u5730\u5716\u529f\u80fd\u7684\u61c9\u7528\u8d8a\u4f86\u8d8a\u666e\u904d\u3002\u900f\u904e MKMapView\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u67e5\u8a62\u7d93\u7def\u5ea6\u4e26\u5b9a\u4f4d\u5230\u7576\u524d\u5ea7\u6a19\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u7d22\u5982\u4f55\u4f7f\u7528 MKMapView \u63d2\u5165\u5927\u982d\u91dd\uff0c\u4e26\u67e5\u8a62\u81ea\u8eab\u7684 GPS \u5ea7\u6a19\u3002<\/p>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/PfFd6gj.gif\" alt=\"Swift\u5730\u5716 \u63d2\u5927\u982d\u91dd \u67e5\u8a62\u81ea\u5df1\u5ea7\u6a19 - \u793a\u7bc4\" title=\"\"><\/p>\n<h2>MKMapView \u7684\u57fa\u672c\u529f\u80fd<\/h2>\n<p>MKMapView \u662f iOS \u7684\u539f\u751f\u5730\u5716\u7d44\u4ef6\uff0c\u63d0\u4f9b\u4ee5\u4e0b\u57fa\u672c\u529f\u80fd\uff1a<br \/>\n&#8211; \u6aa2\u8996\u81ea\u8eab\u5ea7\u6a19<br \/>\n&#8211; \u6a19\u8a18\u4f4d\u7f6e<br \/>\n&#8211; \u5c0e\u822a\u529f\u80fd<\/p>\n<p>\u96d6\u7136\u8a31\u591a\u7528\u6236\u7fd2\u6163\u4f7f\u7528 Google Map\uff0c\u4f46 MKMapView \u4ecd\u7136\u6709\u5176\u7368\u7279\u7684\u512a\u52e2\uff0c\u4f8b\u5982\u53ef\u4ee5\u76f4\u63a5\u900f\u904e Siri \u9032\u884c\u8a9e\u97f3\u5c0e\u822a\u3002<\/p>\n<h2>\u4f7f\u7528 MKMapView \u7684\u6b65\u9a5f<\/h2>\n<p>\u8981\u5728 Swift \u4e2d\u4f7f\u7528 MKMapView\uff0c\u6211\u5011\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u521d\u59cb\u5316 MKMapView<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728\u60a8\u7684\u8996\u5716\u63a7\u5236\u5668\u4e2d\u521d\u59cb\u5316 MKMapView \u4e26\u8a2d\u7f6e\u5176\u4ee3\u7406\u3002<\/p>\n<p>&#8220;`swift<br \/>\nvar locationManager: CLLocationManager!<br \/>\nvar mapView: MKMapView!<\/p>\n<p>override func viewDidLoad() {<br \/>\n    super.viewDidLoad()<\/p>\n<p>    mapView = MKMapView()<br \/>\n    mapView.delegate = self<br \/>\n    mapView.frame = self.view.bounds<br \/>\n    self.view.addSubview(mapView)<\/p>\n<p>    locationManager = CLLocationManager()<br \/>\n    locationManager.delegate = self<br \/>\n    locationManager.desiredAccuracy = kCLLocationAccuracyBest<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>2. \u8acb\u6c42 GPS \u6b0a\u9650<\/h3>\n<p>\u5728\u4f7f\u7528 GPS \u529f\u80fd\u4e4b\u524d\uff0c\u5fc5\u9808\u8acb\u6c42\u7528\u6236\u7684\u6388\u6b0a\u3002\u9019\u53ef\u4ee5\u5728 `viewWillAppear` \u4e2d\u9032\u884c\u3002<\/p>\n<p>&#8220;`swift<br \/>\noverride func viewWillAppear(_ animated: Bool) {<br \/>\n    super.viewWillAppear(animated)<br \/>\n    if CLLocationManager.authorizationStatus() == .notDetermined {<br \/>\n        locationManager.requestWhenInUseAuthorization()<br \/>\n        locationManager.startUpdatingLocation()<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>3. \u76e3\u807d\u4f4d\u7f6e\u66f4\u65b0<\/h3>\n<p>\u5be6\u4f5c CLLocationManagerDelegate\uff0c\u4ee5\u63a5\u6536\u4f4d\u7f6e\u66f4\u65b0\u4e26\u5728\u5730\u5716\u4e0a\u986f\u793a\u7576\u524d\u4f4d\u7f6e\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {<br \/>\n    guard let currentLocation = locations.first else { return }<\/p>\n<p>    let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)<br \/>\n    let region = MKCoordinateRegion(center: currentLocation.coordinate, span: span)<br \/>\n    mapView.setRegion(region, animated: true)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>4. \u6a19\u8a18\u4f4d\u7f6e<\/h3>\n<p>\u4f7f\u7528 MKPointAnnotation \u6a19\u8a18\u7279\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet annotation = MKPointAnnotation()<br \/>\nannotation.coordinate = CLLocation(latitude: 25.034012, longitude: 121.56446).coordinate<br \/>\nannotation.title = &#8220;\u53f0\u5317101&#8221;<br \/>\nannotation.subtitle = &#8220;\u8457\u540d\u5730\u6a19&#8221;<br \/>\nmapView.addAnnotation(annotation)<br \/>\n&#8220;`<\/p>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20191004\/20112271q40sz39FPJ.png\" alt=\"Swift\u5730\u5716 \u63d2\u5927\u982d\u91dd \u67e5\u8a62\u81ea\u5df1\u5ea7\u6a19 - \u53f0\u5317101\u6a19\u8a18\u793a\u7bc4\" title=\"\"><\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<br \/>\n&#8211; **GPS \u6b0a\u9650\u672a\u6388\u4e88**\uff1a\u78ba\u4fdd\u5728 Info.plist \u4e2d\u914d\u7f6e\u9069\u7576\u7684\u63cf\u8ff0\uff08`Privacy &#8211; Location When In Use Usage Description`\uff09\u3002<br \/>\n&#8211; **\u5730\u5716\u672a\u986f\u793a**\uff1a\u78ba\u8a8d MKMapView \u662f\u5426\u6b63\u78ba\u6dfb\u52a0\u5230\u8996\u5716\u5c64\u6b21\u7d50\u69cb\u4e2d\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u61c9\u7528\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u8ffd\u8e64\u7528\u6236\u8def\u5f91<br \/>\n&#8211; \u986f\u793a\u5468\u908a\u7684\u5546\u5bb6\u8cc7\u8a0a<br \/>\n&#8211; \u6574\u5408\u5c0e\u822a\u529f\u80fd<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u672c\u7bc7\u6587\u7ae0\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u719f\u6089\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 MKMapView\uff0c\u4e26\u63d2\u5165\u5927\u982d\u91dd\u53ca\u67e5\u8a62\u7576\u524d\u7684 GPS \u5ea7\u6a19\u3002\u9019\u4e9b\u529f\u80fd\u5c0d\u65bc\u8a31\u591a\u61c9\u7528\u4f86\u8aaa\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\uff0c\u80fd\u5920\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: MKMapView \u7684\u4f7f\u7528\u9700\u8981\u4ec0\u9ebc\u6b0a\u9650\uff1f**<br \/>\nA1: \u4f7f\u7528 MKMapView \u9700\u8981\u7528\u6236\u6388\u4e88\u4f4d\u7f6e\u670d\u52d9\u7684\u6b0a\u9650\uff0c\u60a8\u9700\u8981\u5728 Info.plist \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4f4d\u7f6e\u4f7f\u7528\u8aaa\u660e\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6a19\u8a18\u591a\u500b\u4f4d\u7f6e\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u5275\u5efa\u591a\u500b MKPointAnnotation \u5be6\u4f8b\uff0c\u4e26\u5c07\u5b83\u5011\u6dfb\u52a0\u5230 MKMapView\u3002<\/p>\n<p>**Q3: MKMapView \u652f\u63f4\u54ea\u4e9b\u5730\u5716\u985e\u578b\uff1f**<br \/>\nA3: MKMapView \u652f\u63f4\u6a19\u6e96\u5730\u5716\u3001\u885b\u661f\u5730\u5716\u53ca\u6df7\u5408\u5730\u5716\u7b49\u591a\u7a2e\u986f\u793a\u6a21\u5f0f\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0b\u4f86\u66f4\u8a73\u7d30\u7684\u9910\u5ef3\u8cc7\u8a0a<br \/>\n\u9ede\u9032\u53bb\u9910\u5ef3\u5730\u5740\u4ee5\u5f8c<br \/>\n\u5c31\u6703\u8df3\u8f49\u5230\u5730\u5716\u9801\u9762<br \/>\n\u4f46\u4f7f\u7528\u7684\u5176\u5be6`\u4e0d\u662f\u539f\u751f\u5730\u5716`<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6P","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/423","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=423"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/423\/revisions\/1027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}