{"id":7471,"date":"2025-06-03T14:33:10","date_gmt":"2025-06-05T07:26:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7471"},"modified":"2025-06-05T14:33:10","modified_gmt":"2025-06-05T07:26:10","slug":"https-badgameshow-com-steven-406","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-406\/","title":{"rendered":"Swift Core Location \u5b9a\u4f4d\u529f\u80fd\u5b8c\u5168\u6307\u5357 &#8211; 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, Core Location, \u5b9a\u4f4d\u529f\u80fd, \u61c9\u7528, 2025, iOS\"><\/p>\n<h1>Swift Core Location \u5b9a\u4f4d\u529f\u80fd\u5b8c\u5168\u6307\u5357<\/h1>\n<p>Swift Core Location \u5b9a\u4f4d\u529f\u80fd\u662f\u4e00\u500b\u5f37\u5927\u7684\u6846\u67b6\uff0c\u5141\u8a31\u958b\u767c\u8005\u8f15\u9b06\u5730\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528\u5b9a\u4f4d\u529f\u80fd\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Core Location \u6846\u67b6\u4f86\u7372\u53d6\u8a2d\u5099\u7684\u4f4d\u7f6e\u8cc7\u8a0a\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u8207\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>Core Location \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Core Location \u63d0\u4f9b\u4e00\u5957\u7c21\u55ae\u7684 API\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u7372\u53d6\u8a2d\u5099\u7684\u7d93\u7def\u5ea6\u3001\u6d77\u62d4\u3001\u65b9\u5411\u548c\u901f\u5ea6\u7b49\u8cc7\u8a0a\uff0c\u9084\u80fd\u9032\u4e00\u6b65\u7372\u53d6\u8a73\u7d30\u7684\u5730\u7406\u4f4d\u7f6e\u8cc7\u8a0a\uff0c\u4f8b\u5982\u8857\u9053\u5730\u5740\u548c\u57ce\u5e02\u540d\u7a31\u3002\u9019\u4e9b\u529f\u80fd\u5c0d\u65bc\u8a31\u591a\u61c9\u7528\u7a0b\u5f0f\u4f86\u8aaa\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\uff0c\u4f8b\u5982\u5730\u5716\u5c0e\u822a\u3001\u793e\u4ea4\u5a92\u9ad4\u5b9a\u4f4d\u548c\u5730\u7406\u6a19\u8a18\u7b49\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Core Location<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5be6\u73fe\u5b9a\u4f4d\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import CoreLocation\n\nclass ViewController: UIViewController, CLLocationManagerDelegate {\n    let locationManager = CLLocationManager()\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ \u8a2d\u5b9a\u5b9a\u4f4d\u670d\u52d9\u6388\u6b0a\n        locationManager.requestWhenInUseAuthorization()\n        \/\/ \u8a2d\u5b9a\u5b9a\u4f4d\u7cbe\u78ba\u5ea6\n        locationManager.desiredAccuracy = kCLLocationAccuracyBest\n        \/\/ \u8a2d\u5b9a\u5b9a\u4f4d\u983b\u7387\n        locationManager.distanceFilter = kCLDistanceFilterNone\n        \/\/ \u8a2d\u5b9a\u59d4\u6d3e\u5c0d\u8c61\n        locationManager.delegate = self\n        \/\/ \u958b\u59cb\u5b9a\u4f4d\n        locationManager.startUpdatingLocation()\n    }\n\n    \/\/ \u5b9a\u4f4d\u66f4\u65b0\u6642\u89f8\u767c\n    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n        guard let location = locations.last else { return }\n        \/\/ \u53d6\u5f97\u7d93\u7def\u5ea6\n        let coordinate = location.coordinate\n        \/\/ \u53d6\u5f97\u6d77\u62d4\n        let altitude = location.altitude\n        \/\/ \u53d6\u5f97\u65b9\u5411\n        let course = location.course\n        \/\/ \u53d6\u5f97\u901f\u5ea6\n        let speed = location.speed\n\n        \/\/ \u9019\u88e1\u53ef\u4ee5\u5c07\u4f4d\u7f6e\u8cc7\u8a0a\u986f\u793a\u5728UI\u4e0a\n        print(\"\u7d93\u7def\u5ea6: \\(coordinate.latitude), \\(coordinate.longitude)\")\n        print(\"\u6d77\u62d4: \\(altitude) \u516c\u5c3a\")\n        print(\"\u65b9\u5411: \\(course) \u5ea6\")\n        print(\"\u901f\u5ea6: \\(speed) m\/s\")\n    }\n\n    \/\/ \u932f\u8aa4\u8655\u7406\n    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {\n        print(\"\u5b9a\u4f4d\u5931\u6557: \\(error.localizedDescription)\")\n    }\n}<\/code><\/pre>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54<\/h2>\n<h3>Q1: \u5982\u4f55\u7372\u5f97\u7528\u6236\u7684\u6388\u6b0a\uff1f<\/h3>\n<p>\u5728 iOS \u4e2d\uff0c\u60a8\u9700\u8981\u5728\u61c9\u7528\u7a0b\u5f0f\u7684 Info.plist \u6a94\u6848\u4e2d\u6dfb\u52a0\u5b9a\u4f4d\u670d\u52d9\u7684\u63cf\u8ff0\uff0c\u7136\u5f8c\u4f7f\u7528 <code>requestWhenInUseAuthorization()<\/code> \u6216 <code>requestAlwaysAuthorization()<\/code> \u4f86\u8acb\u6c42\u7528\u6236\u7684\u6388\u6b0a\u3002<\/p>\n<h3>Q2: \u70ba\u4ec0\u9ebc\u5b9a\u4f4d\u529f\u80fd\u4e0d\u5de5\u4f5c\uff1f<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u60a8\u5df2\u7372\u5f97\u7528\u6236\u7684\u6388\u6b0a\u3002\u6b64\u5916\uff0c\u6aa2\u67e5\u8a2d\u5099\u7684\u5b9a\u4f4d\u670d\u52d9\u662f\u5426\u5df2\u555f\u7528\uff0c\u4e26\u78ba\u8a8d\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u5728\u80cc\u666f\u6a21\u5f0f\u4e0b\u662f\u5426\u6709\u6b0a\u9650\u4f7f\u7528\u5b9a\u4f4d\u670d\u52d9\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8655\u7406\u5b9a\u4f4d\u670d\u52d9\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 <code>locationManager(_:didFailWithError:)<\/code> \u65b9\u6cd5\u4e2d\u8655\u7406\u932f\u8aa4\uff0c\u4e26\u5411\u7528\u6236\u986f\u793a\u9069\u7576\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u5e6b\u52a9\u4ed6\u5011\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-Core-Location\u5b9a\u4f4d\u529f\u80fd\u61c9\u7528_1675476405.196557.webp\" alt=\"Swift Core Location \u5b9a\u4f4d\u529f\u80fd\u61c9\u7528\" title=\"Swift Core Location\u3001\u5b9a\u4f4d\u529f\u80fd\u3001\u61c9\u7528\u7a0b\u5f0f\u3001\u958b\u767c\u3001\u61c9\u7528\" width=\"1200\" height=\"628\"\/><\/p>\n<p>\u900f\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u73fe\u5728\u61c9\u8a72\u80fd\u5920\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u6210\u529f\u5be6\u73fe\u5b9a\u4f4d\u529f\u80fd\u3002\u6301\u7e8c\u63a2\u7d22 Core Location \u7684\u5176\u4ed6\u61c9\u7528\uff0c\u5982\u5730\u7406\u570d\u6b04 (Geofencing) \u548c\u4f4d\u7f6e\u8ffd\u8e64\uff0c\u4ee5\u63d0\u5347\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u9ad4\u9a57\uff01<\/p>\n<h2>\u5ef6\u4f38\u95b1\u8b80<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.raywenderlich.com\/5247-core-location-tutorial-for-ios-tracking-visited-locations\" target=\"_blank\" rel=\"noopener\">Core Location Tutorial for iOS: Tracking Visited Locations<\/a><\/li>\n<li><a href=\"https:\/\/www.appcoda.com\/core-location-framework-introduction\/\" target=\"_blank\" rel=\"noopener\">An Introduction to Core Location Framework<\/a><\/li>\n<li><a href=\"https:\/\/www.hackingwithswift.com\/example-code\/location\/how-to-track-a-users-location-with-core-location\" target=\"_blank\" rel=\"noopener\">How to track a user\u2019s location with Core Location<\/a><\/li>\n<li><a href=\"https:\/\/www.raywenderlich.com\/548-core-location-tutorial-for-ios-tracking-a-users-location\" target=\"_blank\" rel=\"noopener\">Core Location Tutorial for iOS: Tracking a User\u2019s Location<\/a><\/li>\n<li><a href=\"https:\/\/www.raywenderlich.com\/5678-core-location-tutorial-for-ios-geofencing\" target=\"_blank\" rel=\"noopener\">Core Location Tutorial for iOS: Geofencing<\/a><\/li>\n<\/ul>\n<p>&#8212;<\/p>\n<p>\u9019\u6bb5\u512a\u5316\u5f8c\u7684\u5167\u5bb9\u4e0d\u50c5\u7b26\u5408 SEO \u6700\u4f73\u5be6\u8e10\uff0c\u9084\u63d0\u4f9b\u4e86\u5be6\u7528\u7684\u6559\u5b78\u6d41\u7a0b\u548c\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff0c\u4ee5\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3 Swift Core Location \u7684\u61c9\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39SwiftCoreLocation\u5b9a\u4f4d\u529f\u80fd\u7684\u61c9\u7528\uff0c\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftCoreLocation\u5b9a\u4f4d\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":7470,"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-7471","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-Core-Location\u5b9a\u4f4d\u529f\u80fd\u61c9\u7528_1675476405.196557.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Wv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7471","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=7471"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7471\/revisions"}],"predecessor-version":[{"id":13620,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7471\/revisions\/13620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7470"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}