{"id":361,"date":"2025-02-20T23:21:00","date_gmt":"2025-02-20T15:21:00","guid":{"rendered":"https:\/\/mille.in\/?p=361"},"modified":"2025-02-21T13:21:08","modified_gmt":"2025-02-21T05:21:08","slug":"spm%e5%af%a6%e6%88%b0%ef%bc%9a%e5%b0%81%e8%a3%9d-navigationkit-%e4%bb%a5%e6%8f%90%e5%8d%87%e5%b0%8e%e8%88%aa%e6%a8%a1%e5%a1%8a%e7%9a%84%e5%8f%af%e5%be%a9%e7%94%a8%e6%80%a7","status":"publish","type":"post","link":"https:\/\/mille.in\/?p=361","title":{"rendered":"SPM\u5be6\u6230\uff1a\u5c01\u88dd NavigationKit \u4ee5\u63d0\u5347\u5c0e\u822a\u6a21\u584a\u7684\u53ef\u5fa9\u7528\u6027"},"content":{"rendered":"\n<p><em>\u6642\u9694\u4e5d\u5e74\uff0c\u7e7c\u7e8c\u6298\u9a30&#8230;<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u9019\u6b21\u6298\u9a30\u4e86\u500b\u4ec0\u9ebc\u6771\u897f\uff1f<\/strong><\/h4>\n\n\n\n<p>\u5728 iOS \u958b\u767c\u88e1\uff0c<strong>\u5b9a\u4f4d\u8ddf\u5c0e\u822a<\/strong>&nbsp;\u662f\u4e0d\u5c11 App \u7684\u5fc5\u5099\u529f\u80fd\uff0c\u7121\u8ad6\u662f\u5916\u9001\u3001\u9a0e\u884c\u3001\u65c5\u904a\u3001\u751a\u81f3\u53ea\u662f\u55ae\u7d14\u7684\u904b\u52d5\u8a18\u9304\uff0c\u5e7e\u4e4e\u90fd\u96e2\u4e0d\u958b GPS\u3002\u4f46\u5982\u679c\u4f60\u76f4\u63a5\u7528&nbsp;<code>CoreLocation<\/code>&nbsp;\u548c&nbsp;<code>MapKit<\/code>\uff0c\u4f60\u5f88\u5feb\u5c31\u6703\u767c\u73fe\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ee3\u78bc\u5230\u8655\u90fd\u662f<\/strong>\uff1a\u6bcf\u500b\u6a21\u7d44\u90fd\u8981\u8acb\u6c42\u5b9a\u4f4d\u3001\u8a08\u7b97\u8def\u7dda\uff0c\u91cd\u8907\u5beb\u5f97\u5fc3\u7169\u3002<\/li>\n\n\n\n<li><strong>\u6e2c\u8a66\u56f0\u96e3<\/strong>\uff1a<code>CLLocationManager<\/code>&nbsp;\u548c&nbsp;<code>MKDirections<\/code>&nbsp;\u9019\u4e9b API \u592a\u300c\u7cfb\u7d71\u7d1a\u300d\uff0c\u6c92\u8fa6\u6cd5\u597d\u597d Mock\uff0c\u55ae\u5143\u6e2c\u8a66\u8b8a\u6210\u5730\u7344\u3002<\/li>\n\n\n\n<li><strong>\u64f4\u5c55\u6027\u5dee<\/strong>\uff1a\u5982\u679c\u54ea\u5929\u60f3\u63a5&nbsp;<strong>Google Maps<\/strong>&nbsp;\u6216&nbsp;<strong>Mapbox<\/strong>\uff0c\u5f88\u53ef\u80fd\u5f97\u628a\u539f\u4f86\u7684\u4ee3\u78bc\u5927\u5378\u516b\u584a\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u70ba\u4e86\u8b93\u9019\u4e9b\u554f\u984c\u8fce\u5203\u800c\u89e3\uff0c\u6211\u5011\u5c01\u88dd\u4e86\u4e00\u500b&nbsp;<strong>\u6a21\u7d44\u5316\u7684 iOS \u5c0e\u822a SDK\u2014\u2014<code>NavigationKit<\/code><\/strong>\uff0c\u5b83\u628a&nbsp;<strong>\u5b9a\u4f4d<\/strong>\u3001<strong>\u8def\u7dda\u898f\u5283<\/strong>\u3001<strong>\u5730\u5716\u986f\u793a<\/strong>&nbsp;\u5206\u958b\u7ba1\u7406\uff0c\u4e26\u4e14\u63a1\u7528&nbsp;<strong>Swift Package Manager\uff08SPM\uff09<\/strong>\uff0c\u8b93\u6574\u500b\u6d41\u7a0b\u4e7e\u6de8\u3001\u6e05\u6670\u3001\u597d\u7dad\u8b77\u3002<\/p>\n\n\n\n<p>\u9019\u7bc7\u6587\u7ae0\u5c31\u4f86\u804a\u804a\u9019\u6b21\u7684&nbsp;<strong>\u5c01\u88dd\u904e\u7a0b\u3001\u8e29\u5751\u7d93\u9a57\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6709\u8da3\u7684\u7d30\u7bc0<\/strong>\uff0c\u770b\u770b\u600e\u9ebc\u628a\u5c0e\u822a\u6a21\u7d44\u505a\u5f97\u66f4\u512a\u96c5\uff01\ud83d\ude0e<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u67b6\u69cb\u8a2d\u8a08<\/strong><\/h2>\n\n\n\n<p>\u5728\u958b\u59cb\u5beb\u4ee3\u78bc\u4e4b\u524d\uff0c\u5148\u60f3\u60f3\u600e\u9ebc\u62c6\u6a21\u7d44\u6bd4\u8f03\u5408\u7406\u3002\u9019\u6b21\u6211\u5011\u628a&nbsp;<code>NavigationKit<\/code>&nbsp;\u62c6\u6210\u4e09\u500b\u90e8\u5206\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>LocationKit<\/code><\/strong>&nbsp;\u2014\u2014 \u8ca0\u8cac&nbsp;<strong>\u8a2d\u5099\u5b9a\u4f4d<\/strong>\uff0c\u5c01\u88dd&nbsp;<code>CLLocationManager<\/code>\uff0c\u4e26\u4e14\u652f\u63f4 SwiftUI \u7d81\u5b9a\u3002<\/li>\n\n\n\n<li><strong><code>RouteKit<\/code><\/strong>&nbsp;\u2014\u2014 \u8ca0\u8cac&nbsp;<strong>\u8def\u7dda\u898f\u5283 &amp; \u8ecc\u8de1\u8a18\u9304<\/strong>\uff0c\u5c01\u88dd&nbsp;<code>MKDirections<\/code>\uff0c\u63d0\u4f9b\u66f4\u7c21\u55ae\u7684 API\u3002<\/li>\n\n\n\n<li><strong><code>MapKitWrapper<\/code><\/strong>&nbsp;\u2014\u2014&nbsp;<strong>\u5305\u88dd&nbsp;<code>MapKit<\/code>&nbsp;UI \u76f8\u95dc\u908f\u8f2f<\/strong>\uff0c\u8b93 SwiftUI \u76f4\u63a5\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>NavigationKit\/<br>\u251c\u2500\u2500 Sources\/<br>\u2502 \u251c\u2500\u2500 NavigationKit\/ # \u5165\u53e3\u6a21\u7d44<br>\u2502 \u251c\u2500\u2500 LocationKit\/ # \u5b9a\u4f4d\u529f\u80fd<br>\u2502 \u251c\u2500\u2500 RouteKit\/ # \u8def\u5f91\u8a08\u7b97<br>\u2502 \u251c\u2500\u2500 MapKitWrapper\/ # MapKit UI \u5c01\u88dd<br>\u251c\u2500\u2500 Tests\/<br>\u2502 \u251c\u2500\u2500 NavigationKitTests\/<br>\u2502 \u251c\u2500\u2500 LocationKitTests\/<br>\u2502 \u251c\u2500\u2500 RouteKitTests\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u9019\u9ebc\u62c6\u7684\u597d\u8655<\/strong><\/h3>\n\n\n\n<p><strong>\u4f4e\u8026\u5408<\/strong>\uff1a\u6bcf\u500b\u6a21\u7d44\u53ea\u5c08\u6ce8\u4e00\u4ef6\u4e8b\uff0c\u907f\u514d\u4e00\u5768&nbsp;<code>NavigationManager<\/code>&nbsp;\u795e\u7d1a\u985e\u5225\u3002<br><strong>\u6613\u64f4\u5c55<\/strong>\uff1a\u672a\u4f86\u5982\u679c\u8981\u63a5 Google Maps\uff0c\u53ea\u8981\u6539&nbsp;<code>MapKitWrapper<\/code>\uff0c\u4e0d\u7528\u52d5&nbsp;<code>LocationKit<\/code>&nbsp;\u6216&nbsp;<code>RouteKit<\/code>\u3002<br><strong>SwiftUI \u53cb\u5584<\/strong>\uff1a\u5167\u5efa&nbsp;<code>ObservableObject<\/code>\uff0c\u8b93\u6578\u64da\u6d41\u66f4\u81ea\u7136\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><code>LocationKit<\/code>\u2014\u2014 \u4e7e\u6de8\u4fd0\u843d\u7684\u5b9a\u4f4d API<\/strong><\/h2>\n\n\n\n<p><code>CLLocationManager<\/code>&nbsp;\u9019\u73a9\u610f\u5152\u4e0d\u597d\u7528\uff0c\u56e0\u70ba\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9700\u8981\u8655\u7406&nbsp;<strong>\u6388\u6b0a<\/strong>\u3001<strong>\u80cc\u666f\u6a21\u5f0f<\/strong>\u3001<strong>\u6e96\u78ba\u5ea6\u8a2d\u5b9a<\/strong>\uff0c\u4ee3\u78bc\u5f88\u7463\u788e\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u76f4\u63a5\u585e\u5230\u8996\u5716\u88e1\u9762\uff0cUI \u5c64\u5c31\u8b8a\u5f97\u5f88\u96dc\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u4ee5\u6211\u5011\u5c01\u88dd\u4e86\u4e00\u500b&nbsp;<code>LocationService<\/code>&nbsp;\u4f86\u7c21\u5316\u9019\u4e9b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import Foundationimport CoreLocation\n\npublic final class LocationService: NSObject, ObservableObject {\n    private let locationManager = CLLocationManager()\n    @Published public private(set) var currentLocation: CLLocation?\n\n    public override init() {\n        super.init()\n        locationManager.delegate = self\n        locationManager.desiredAccuracy = kCLLocationAccuracyBest\n    }\n\n    public func startUpdatingLocation() {\n        locationManager.requestWhenInUseAuthorization()\n        locationManager.startUpdatingLocation()\n    }\n}\n\nextension LocationService: CLLocationManagerDelegate {\n    public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: &#91;CLLocation]) {\n        self.currentLocation = locations.last\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<p><strong>\u70ba\u4ec0\u9ebc\u9019\u9ebc\u5beb\uff1f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>@Published<\/code>&nbsp;\u8b93 SwiftUI \u8996\u5716\u53ef\u4ee5\u76f4\u63a5\u8a02\u95b1&nbsp;<code>currentLocation<\/code>\uff0c\u66f4\u65b0\u754c\u9762\u4e0d\u7528 callback\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u66b4\u9732&nbsp;<code>CLLocationManager<\/code><\/strong>\uff0c\u8b93&nbsp;<code>LocationService<\/code>&nbsp;\u8ca0\u8cac\u4e00\u5207\u5e95\u5c64\u7d30\u7bc0\uff0c\u5225\u7684\u6a21\u7d44\u53ea\u7ba1\u62ff\u6578\u64da\u3002<\/li>\n\n\n\n<li><strong>\u672a\u4f86\u8981\u64f4\u5c55\u80cc\u666f\u6a21\u5f0f\u3001\u5730\u7406\u570d\u6b04\uff08Geofencing\uff09\u6642\uff0c\u9019\u500b\u67b6\u69cb\u6703\u66f4\u9748\u6d3b\u3002<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><code>RouteKit<\/code>\u2014\u2014 \u4e0d\u518d\u624b\u5beb&nbsp;<code>MKDirections<\/code><\/strong><\/h2>\n\n\n\n<p><strong>iOS \u5167\u5efa&nbsp;<code>MKDirections<\/code>&nbsp;\u53ef\u4ee5\u8a08\u7b97\u8def\u7dda\uff0c\u4f46 API \u592a\u4f4e\u968e\uff0c\u9084\u5f97\u81ea\u5df1\u8655\u7406\u8acb\u6c42\u3001\u89e3\u6790\u7d50\u679c\u3002<\/strong><br>\u6211\u5011\u5e0c\u671b\u76f4\u63a5\u4f86\u500b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>routeManager.calculateRoute(from: \u8d77\u9ede, to: \u7d42\u9ede) { route in\n    \u986f\u793a\u8def\u7dda\n}\n<\/code><\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u6211\u5011\u5c01\u88dd\u4e86&nbsp;<code>RouteManager<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import MapKit\n\npublic final class RouteManager {\n    public func calculateRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D, completion: @escaping (MKRoute?) -&gt; Void) {\n        let request = MKDirections.Request()\n        request.source = MKMapItem(placemark: MKPlacemark(coordinate: source))\n        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: destination))\n        request.transportType = .automobile\n\n        MKDirections(request: request).calculate { response, error in\n            completion(response?.routes.first)\n        }\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<p><strong>\u9019\u6a23\u5c01\u88dd\u7684\u597d\u8655<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ee3\u78bc\u6e05\u723d<\/strong>\uff1a\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u624b\u5beb&nbsp;<code>MKDirections.Request()<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u64f4\u5c55\u6027\u5f37<\/strong>\uff1a\u5982\u679c\u4e4b\u5f8c\u8981\u652f\u63f4 Google Maps\uff0c\u53ea\u8981\u6539&nbsp;<code>RouteManager<\/code>\uff0c\u8abf\u7528\u65b9\u4e0d\u7528\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SPM\u5316<\/strong><\/h2>\n\n\n\n<p>\u65e2\u7136\u8981\u6a21\u7d44\u5316\uff0c\u7576\u7136\u8981\u8b93&nbsp;<code>NavigationKit<\/code>&nbsp;\u80fd\u5920&nbsp;<strong>\u50cf\u6b63\u898f SDK \u4e00\u6a23\u4f7f\u7528<\/strong>\u3002\u6240\u4ee5\u6211\u5011\u7528&nbsp;<strong>Swift Package Manager\uff08SPM\uff09<\/strong>&nbsp;\u4f86\u7ba1\u7406\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>swift package init --type library\n<\/code><\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c&nbsp;<code>Package.swift<\/code>&nbsp;\u9577\u9019\u6a23\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>let package = Package(\n    name: \"NavigationKit\",\n    platforms: &#91;.iOS(.v15)],\n    products: &#91;\n        .library(name: \"NavigationKit\", targets: &#91;\"NavigationKit\"])\n    ],\n    dependencies: &#91;],\n    targets: &#91;\n        .target(name: \"NavigationKit\", dependencies: &#91;]),\n        .testTarget(name: \"NavigationKitTests\", dependencies: &#91;\"NavigationKit\"]),\n    ]\n)\n<\/code><\/code><\/pre>\n\n\n\n<p>\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5&nbsp;<strong>\u76f4\u63a5\u5728\u5176\u4ed6\u5c08\u6848\u4e2d\u7528&nbsp;<code>NavigationKit<\/code>\uff0c\u800c\u4e0d\u7528\u6bcf\u6b21 copy-paste \u4ee3\u78bc\u4e86\u3002<\/strong>&nbsp;\ud83c\udf89<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u7d50\u8ad6<\/strong><\/h2>\n\n\n\n<p>\u9019\u6b21\u5c01\u88dd&nbsp;<code>NavigationKit<\/code>\uff0c\u8b93 iOS \u7684\u5c0e\u822a\u529f\u80fd\u8b8a\u5f97\u66f4&nbsp;<strong>\u6a21\u7d44\u5316\u3001\u6e05\u6670\u3001\u6613\u7528<\/strong>\uff1a <\/p>\n\n\n\n<p>\u2714&nbsp;<strong><code>LocationKit<\/code>&nbsp;\u63d0\u4f9b SwiftUI \u53cb\u5584\u7684 GPS \u5b9a\u4f4d API<\/strong><br>\u2714&nbsp;<strong><code>RouteKit<\/code>&nbsp;\u8b93\u8def\u7dda\u8a08\u7b97\u8b8a\u5f97\u7c21\u55ae\u76f4\u89ba<\/strong><br>\u2714&nbsp;<strong>Swift Package \u8b93\u6574\u500b SDK \u53ef\u91cd\u7528\u3001\u6613\u7dad\u8b77<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd1c \u4e0b\u4e00\u6b65\u8a08\u756b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u652f\u63f4 Google Maps &amp; Mapbox<\/strong><\/li>\n\n\n\n<li><strong>\u52a0\u5165\u96e2\u7dda\u5c0e\u822a\u529f\u80fd<\/strong><\/li>\n\n\n\n<li><strong>\u8b93&nbsp;<code>RouteKit<\/code>&nbsp;\u652f\u63f4\u66f4\u591a\u985e\u578b\u7684\u8def\u5f91\u898f\u5283\uff08\u6b65\u884c\u3001\u9a0e\u884c\u3001\u8ca8\u8eca\u8def\u7dda\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u9019\u6b21\u7684\u5c01\u88dd\u7b97\u662f\u89e3\u6c7a\u4e86\u5f88\u591a&nbsp;<strong>\u5c0e\u822a API \u4f7f\u7528\u4e0a\u7684\u75db\u9ede<\/strong>\uff0c\u5982\u679c\u4f60\u4e5f\u6709\u985e\u4f3c\u7684\u9700\u6c42\uff0c\u6216\u8005\u5c0d iOS \u958b\u767c\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u4ea4\u6d41\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6642\u9694\u4e5d\u5e74\uff0c\u7e7c\u7e8c\u6298\u9a30&#8230; \u9019\u6b21\u6298&hellip;<\/p>\n","protected":false},"author":1,"featured_media":362,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[8],"tags":[66,67,68],"class_list":["post-361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-apple","tag-swift","tag-swift-package-manager"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mille.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=361"}],"version-history":[{"count":2,"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/posts\/361\/revisions\/364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mille.in\/index.php?rest_route=\/wp\/v2\/media\/362"}],"wp:attachment":[{"href":"https:\/\/mille.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mille.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mille.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}