{"id":6734,"date":"2025-05-31T13:04:01","date_gmt":"2025-06-05T05:06:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6734"},"modified":"2025-06-05T13:04:01","modified_gmt":"2025-06-05T05:06:00","slug":"https-badgameshow-com-steven-45","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-45\/","title":{"rendered":"Swift CoreLocation \u4f7f\u7528\u6307\u5357\uff1a\u6700\u65b0\u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u5be6\u4f5c\uff082025\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, CoreLocation, \u5b9a\u4f4d, \u5730\u5716\u64cd\u4f5c, Swift \u6559\u5b78, iOS \u958b\u767c\"><\/p>\n<h1>Swift CoreLocation \u4f7f\u7528\u6307\u5357\uff1a\u6700\u65b0\u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u5be6\u4f5c\uff082025\uff09<\/h1>\n<p>Swift CoreLocation \u662f\u4e00\u500b\u5f37\u5927\u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u7372\u53d6\u4f7f\u7528\u8005\u7684\u5730\u7406\u4f4d\u7f6e\u3002\u900f\u904e\u4f7f\u7528 CoreLocation\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u529f\u80fd\u6574\u5408\u5230\u60a8\u7684 App \u4e2d\uff0c\u4f7f\u5176\u66f4\u52a0\u5b8c\u6574\u548c\u4e92\u52d5\u3002\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift CoreLocation \u7372\u53d6\u4f7f\u7528\u8005\u4f4d\u7f6e\uff0c\u4e26\u5c07\u8a72\u4f4d\u7f6e\u986f\u793a\u5728\u5730\u5716\u4e0a\u3002<\/p>\n<h2>CoreLocation \u57fa\u672c\u6982\u5ff5<\/h2>\n<p>CoreLocation \u6846\u67b6\u63d0\u4f9b\u4e86\u7372\u53d6\u4f7f\u7528\u8005\u4f4d\u7f6e\u7684\u529f\u80fd\uff0c\u5305\u62ec\u7d93\u7def\u5ea6\u7684\u7372\u53d6\u548c\u5730\u5740\u7684\u8f49\u63db\u3002\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528\u6b64\u6846\u67b6\u4f86\u7372\u53d6\u4f7f\u7528\u8005\u7684\u7576\u524d\u4f4d\u7f6e\uff0c\u4e26\u6839\u64da\u4f4d\u7f6e\u9032\u884c\u76f8\u95dc\u64cd\u4f5c\u3002<\/p>\n<h2>CoreLocation \u5be6\u4f5c\u6d41\u7a0b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 CoreLocation \u7372\u53d6\u4f4d\u7f6e\u4e26\u986f\u793a\u5728\u5730\u5716\u4e0a\u7684\u5b8c\u6574\u5be6\u4f5c\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u914d\u7f6e Info.plist<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 <strong>info.plist<\/strong> \u4e2d\u6dfb\u52a0\u4e00\u500b\u96b1\u79c1\u63cf\u8ff0\u6b04\u4f4d\uff1a<strong>Privacy &#8211; Location When In Use Usage Description<\/strong>\u3002\u9019\u5c07\u63d0\u793a\u4f7f\u7528\u8005\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u8a2a\u554f\u4ed6\u5011\u7684\u4f4d\u7f6e\u3002<\/p>\n<h3>2. \u8a2d\u5b9a CLLocationManager<\/h3>\n<p>\u5728 <strong>ViewController<\/strong> \u4e2d\uff0c\u60a8\u9700\u8981\u521d\u59cb\u5316 <strong>CLLocationManager<\/strong> \u4e26\u8a2d\u7f6e\u5176\u4ee3\u7406\uff1a<\/p>\n<pre><code class=\"language-swift\">let locationManager = CLLocationManager()\nlocationManager.delegate = self<\/code><\/pre>\n<h3>3. \u8acb\u6c42\u4f4d\u7f6e\u6b0a\u9650<\/h3>\n<p>\u5728 <strong>viewDidLoad<\/strong> \u65b9\u6cd5\u4e2d\uff0c\u8acb\u6c42\u4f7f\u7528\u8005\u540c\u610f\u4f7f\u7528\u4f4d\u7f6e\u8cc7\u8a0a\uff1a<\/p>\n<pre><code class=\"language-swift\">locationManager.requestWhenInUseAuthorization()<\/code><\/pre>\n<h3>4. \u7372\u53d6\u4f4d\u7f6e\u66f4\u65b0<\/h3>\n<p>\u5be6\u4f5c <strong>CLLocationManagerDelegate<\/strong> \u7684\u65b9\u6cd5\uff0c\u7576\u4f4d\u7f6e\u66f4\u65b0\u6642\u57f7\u884c\u76f8\u61c9\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift\">func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n    guard let location = locations.last else { return }\n    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)\n    let region = MKCoordinateRegion(center: center, latitudinalMeters: 1000, longitudinalMeters: 1000)\n    mapView.setRegion(region, animated: true)\n}<\/code><\/pre>\n<h3>5. \u6dfb\u52a0 MKMapView<\/h3>\n<p>\u5728 <strong>ViewController<\/strong> \u4e2d\u6dfb\u52a0 <strong>MKMapView<\/strong>\uff0c\u4e26\u8a2d\u7f6e\u5176\u4ee3\u7406\uff1a<\/p>\n<pre><code class=\"language-swift\">let mapView = MKMapView()\nmapView.delegate = self<\/code><\/pre>\n<h3>6. \u986f\u793a\u5730\u5740<\/h3>\n<p>\u4f7f\u7528 <strong>MKMapViewDelegate<\/strong> \u7684\u65b9\u6cd5\u4f86\u986f\u793a\u7576\u524d\u5730\u9ede\u7684\u5730\u5740\uff1a<\/p>\n<pre><code class=\"language-swift\">func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {\n    let center = getCenterLocation(for: mapView)\n    let geoCoder = CLGeocoder()\n    geoCoder.reverseGeocodeLocation(center) { placemarks, error in\n        guard let placemark = placemarks?.first else { return }\n        let streetNumber = placemark.subThoroughfare ?? \"\"\n        let streetName = placemark.thoroughfare ?? \"\"\n        DispatchQueue.main.async {\n            self.addressLabel.text = \"\\(streetNumber) \\(streetName)\"\n        }\n    }\n}\n\nfunc getCenterLocation(for mapView: MKMapView) -> CLLocation {\n    let latitude = mapView.centerCoordinate.latitude\n    let longitude = mapView.centerCoordinate.longitude\n    return CLLocation(latitude: latitude, longitude: longitude)\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u5efa\u8b70<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u6b0a\u9650\u672a\u6b63\u78ba\u8a2d\u5b9a\uff1a\u8acb\u78ba\u4fdd\u5728 <strong>info.plist<\/strong> \u4e2d\u5df2\u6dfb\u52a0\u4f4d\u7f6e\u4f7f\u7528\u63cf\u8ff0\u3002<\/li>\n<li>\u5730\u5716\u672a\u80fd\u986f\u793a\u4f4d\u7f6e\uff1a\u6aa2\u67e5\u662f\u5426\u6709\u6b63\u78ba\u7684\u5730\u5716\u8996\u5716\u8a2d\u7f6e\u548c\u4f4d\u7f6e\u66f4\u65b0\u6b0a\u9650\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift CoreLocation \u4f86\u7372\u53d6\u4f7f\u7528\u8005\u4f4d\u7f6e\u4e26\u986f\u793a\u5728\u5730\u5716\u4e0a\u3002\u901a\u904e\u5728 <strong>info.plist<\/strong> \u4e2d\u6dfb\u52a0\u5fc5\u8981\u7684\u96b1\u79c1\u63cf\u8ff0\uff0c\u4e26\u5728 <strong>ViewController<\/strong> \u4e2d\u5be6\u4f5c CLLocationManager \u548c MKMapView\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u70ba\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u589e\u6dfb\u5b9a\u4f4d\u529f\u80fd\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u8655\u7406\u4f4d\u7f6e\u7372\u53d6\u5931\u6557\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u5728 <strong>locationManager(_:didFailWithError:)<\/strong> \u65b9\u6cd5\u4e2d\u8655\u7406\u932f\u8aa4\uff0c\u4e26\u63d0\u793a\u4f7f\u7528\u8005\u6aa2\u67e5\u4f4d\u7f6e\u670d\u52d9\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a18\uff1f<\/h3>\n<p>A2: \u4f7f\u7528 <strong>MKPointAnnotation<\/strong> \u53ef\u4ee5\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a18\uff0c\u4e26\u4f7f\u7528 <strong>mapView.addAnnotation()<\/strong> \u65b9\u6cd5\u4f86\u986f\u793a\u3002<\/p>\n<h3>Q3: \u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u5730\u5716\u7684\u60c5\u6cc1\u4e0b\u50c5\u7372\u53d6\u4f4d\u7f6e\u55ce\uff1f<\/h3>\n<p>A3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u50c5\u4f7f\u7528 <strong>CLLocationManager<\/strong> \u7372\u53d6\u4f4d\u7f6e\uff0c\u800c\u4e0d\u5fc5\u986f\u793a\u5730\u5716\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-CoreLocation\u4f7f\u7528\u2757\ufe0f-\u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u6559\u5b78_1675394765.306796.webp\" alt=\"Swift CoreLocation\u4f7f\u7528\u2757\ufe0f - \u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u6559\u5b78\" title=\"Swift CoreLocation\u3001\u5b9a\u4f4d\u3001\u5730\u5716\u64cd\u4f5c\u3001Swift \u6559\u5b78\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39SwiftCoreLocation\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5f9e\u5b9a\u4f4d\u5230\u5730\u5716\u64cd\u4f5c\uff0c\u8a73\u7d30\u89e3\u8aaa\u5982\u4f55\u4f7f\u7528Swift\u4f86\u64cd\u4f5c\u5b9a\u4f4d\u8207\u5730\u5716\uff0c\u8b93\u4f60\u5feb\u901f\u4e0a\u624b\uff0c\u8f15\u9b06\u5b78\u7fd2SwiftCoreLocation\u3002<\/p>\n","protected":false},"author":1,"featured_media":6733,"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-6734","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-CoreLocation\u4f7f\u7528\u2757\ufe0f-\u5b9a\u4f4d\u8207\u5730\u5716\u64cd\u4f5c\u6559\u5b78_1675394765.306796.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1KC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6734","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=6734"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6734\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6733"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}