@sargsyansevak/expo-mapbox-navigation 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +21 -0
  3. package/README.md +313 -0
  4. package/android/build.gradle +98 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/expo/modules/mapboxnavigation/ExpoMapboxNavigationModule.kt +120 -0
  7. package/android/src/main/java/expo/modules/mapboxnavigation/ExpoMapboxNavigationView.kt +1100 -0
  8. package/android/src/main/res/drawable-hdpi/icon_compass.png +0 -0
  9. package/android/src/main/res/drawable-hdpi/icon_mute.png +0 -0
  10. package/android/src/main/res/drawable-hdpi/icon_overview.png +0 -0
  11. package/android/src/main/res/drawable-hdpi/icon_sound.png +0 -0
  12. package/android/src/main/res/drawable-hdpi/icon_x.png +0 -0
  13. package/android/src/main/res/drawable-ldpi/icon_compass.png +0 -0
  14. package/android/src/main/res/drawable-ldpi/icon_mute.png +0 -0
  15. package/android/src/main/res/drawable-ldpi/icon_overview.png +0 -0
  16. package/android/src/main/res/drawable-ldpi/icon_sound.png +0 -0
  17. package/android/src/main/res/drawable-ldpi/icon_x.png +0 -0
  18. package/android/src/main/res/drawable-mdpi/icon_compass.png +0 -0
  19. package/android/src/main/res/drawable-mdpi/icon_mute.png +0 -0
  20. package/android/src/main/res/drawable-mdpi/icon_overview.png +0 -0
  21. package/android/src/main/res/drawable-mdpi/icon_sound.png +0 -0
  22. package/android/src/main/res/drawable-mdpi/icon_x.png +0 -0
  23. package/android/src/main/res/drawable-xhdpi/icon_compass.png +0 -0
  24. package/android/src/main/res/drawable-xhdpi/icon_mute.png +0 -0
  25. package/android/src/main/res/drawable-xhdpi/icon_overview.png +0 -0
  26. package/android/src/main/res/drawable-xhdpi/icon_sound.png +0 -0
  27. package/android/src/main/res/drawable-xhdpi/icon_x.png +0 -0
  28. package/android/src/main/res/drawable-xxhdpi/icon_compass.png +0 -0
  29. package/android/src/main/res/drawable-xxhdpi/icon_mute.png +0 -0
  30. package/android/src/main/res/drawable-xxhdpi/icon_overview.png +0 -0
  31. package/android/src/main/res/drawable-xxhdpi/icon_sound.png +0 -0
  32. package/android/src/main/res/drawable-xxhdpi/icon_x.png +0 -0
  33. package/android/src/main/res/drawable-xxxhdpi/icon_compass.png +0 -0
  34. package/android/src/main/res/drawable-xxxhdpi/icon_mute.png +0 -0
  35. package/android/src/main/res/drawable-xxxhdpi/icon_overview.png +0 -0
  36. package/android/src/main/res/drawable-xxxhdpi/icon_sound.png +0 -0
  37. package/android/src/main/res/drawable-xxxhdpi/icon_x.png +0 -0
  38. package/android/src/main/res/values/styles.xml +9 -0
  39. package/app.plugin.js +1 -0
  40. package/build/ExpoMapboxNavigation.types.d.ts +90 -0
  41. package/build/ExpoMapboxNavigation.types.d.ts.map +1 -0
  42. package/build/ExpoMapboxNavigation.types.js +2 -0
  43. package/build/ExpoMapboxNavigation.types.js.map +1 -0
  44. package/build/ExpoMapboxNavigationModule.d.ts +3 -0
  45. package/build/ExpoMapboxNavigationModule.d.ts.map +1 -0
  46. package/build/ExpoMapboxNavigationModule.js +3 -0
  47. package/build/ExpoMapboxNavigationModule.js.map +1 -0
  48. package/build/ExpoMapboxNavigationView.d.ts +5 -0
  49. package/build/ExpoMapboxNavigationView.d.ts.map +1 -0
  50. package/build/ExpoMapboxNavigationView.js +7 -0
  51. package/build/ExpoMapboxNavigationView.js.map +1 -0
  52. package/build/ExpoMapboxNavigationView.web.d.ts +4 -0
  53. package/build/ExpoMapboxNavigationView.web.d.ts.map +1 -0
  54. package/build/ExpoMapboxNavigationView.web.js +7 -0
  55. package/build/ExpoMapboxNavigationView.web.js.map +1 -0
  56. package/build/index.d.ts +4 -0
  57. package/build/index.d.ts.map +1 -0
  58. package/build/index.js +3 -0
  59. package/build/index.js.map +1 -0
  60. package/expo-module.config.json +9 -0
  61. package/ios/ExpoMapboxNavigation.podspec +46 -0
  62. package/ios/ExpoMapboxNavigationModule.swift +92 -0
  63. package/ios/ExpoMapboxNavigationView.swift +509 -0
  64. package/package.json +47 -0
  65. package/plugin/config.js +127 -0
  66. package/src/ExpoMapboxNavigation.types.ts +70 -0
  67. package/src/ExpoMapboxNavigationModule.ts +2 -0
  68. package/src/ExpoMapboxNavigationView.tsx +17 -0
  69. package/src/ExpoMapboxNavigationView.web.tsx +13 -0
  70. package/src/index.ts +11 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## Unpublished
4
+
5
+ ### 🛠 Breaking changes
6
+
7
+ ### 🎉 New features
8
+
9
+ ### 🐛 Bug fixes
10
+
11
+ ### 💡 Others
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 badatgil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # Expo Mapbox Navigation
2
+
3
+ A simple Expo wrapper for Mapbox's navigation SDK's on Android and iOS
4
+
5
+ ## Installation
6
+
7
+ ### `@rnmapbox/maps`
8
+
9
+ This package relies on the installtion of `@rnmapbox/maps`, so you'll have to install and setup as explained in their [installtion instructions](https://rnmapbox.github.io/docs/install). The current version of **Expo Mapbox Navigation** package was developed and tested for Mapbox Maps version `11.11.0`, so that is the recommended version when setting up the maps package. It is possible that everything will work with a later version, but unlikely with a lower version. **However, make sure you explcitly set the version with `RNMapboxMapsVersion` because this package relies on this being set to work**
10
+
11
+ > [!NOTE]
12
+ > Make sure to follow the instructions carefully and include the proper token in the config plugin and to call the `Mapbox.setAccessToken` function. You can be sure everything is good when you're able to render a map.
13
+
14
+ ### Install package
15
+
16
+ **Install via npm (recommended):**
17
+
18
+ ```bash
19
+ npm install @sargsyansevak/expo-mapbox-navigation
20
+ ```
21
+
22
+ Or directly from GitHub:
23
+
24
+ ```json
25
+ // package.json
26
+ "dependencies": {
27
+ "@sargsyansevak/expo-mapbox-navigation": "github:sargsyansevak/expo-mapbox-navigation"
28
+ }
29
+ ```
30
+
31
+ ### Configure package
32
+
33
+ In your `app.json` or `app.js`, you'll need to add a plugin for the package under the `plugins` entry with a valid mapbox access token (The one that starts with `pk.`). For the mapbox maps version, you have to use the same version set in the setup steps of `@rnmapbox/maps`.
34
+
35
+ ```json
36
+ "plugins": [
37
+ ...other plugins
38
+ [
39
+ "@sargsyansevak/expo-mapbox-navigation",
40
+ {
41
+ "accessToken": "<YOUR_TOKEN>",
42
+ "mapboxMapsVersion": "<MAPBOX_MAPS_VERSION>"
43
+ }
44
+ ]
45
+ ]
46
+ ```
47
+
48
+ Additionally you need to enable `useFrameworks` in your ios config. This can be achieved by using the `expo-build-properties` package and plugin as follows:
49
+
50
+ ```json
51
+ "plugins": [
52
+ ...other plugins
53
+ [
54
+ "expo-build-properties",
55
+ {
56
+ "ios": {
57
+ "useFrameworks": "static"
58
+ }
59
+ }
60
+ ],
61
+ ]
62
+ ```
63
+
64
+ #### More notes on plugin
65
+
66
+ The plugin also has a `androidColorOverrides` field which allows you to define resource colors in the android app. This is particualrly useful to override some of the predefined mapbox colors and customize the apperance of the components. It is somewhat tricky to find which colors to change, but looking through the [mapbox repo](https://github.com/mapbox/mapbox-navigation-android/blob/main/ui-components/src/main/res/values/colors-maneuver.xml) may help
67
+
68
+ ```json
69
+ "plugins": [
70
+ ...other plugins
71
+ [
72
+ "@sargsyansevak/expo-mapbox-navigation",
73
+ {
74
+ "accessToken": "<YOUR_TOKEN>",
75
+ "mapboxMapsVersion": "<MAPBOX_MAPS_VERSION>",
76
+ "androidColorOverrides": {
77
+ "mapbox_main_maneuver_background_color": "#FF0000"
78
+ }
79
+ }
80
+ ]
81
+ ]
82
+ ```
83
+
84
+ ### Usage
85
+
86
+ After getting location permissions you can render the navigation map as follows anywhere in the app
87
+
88
+ ```
89
+ <MapboxNavigationView
90
+ style={{ flex: 1 }}
91
+ coordinates={coordinates} />
92
+ ```
93
+
94
+ ## Props
95
+
96
+ #### `coordinates`
97
+
98
+ An array of `{latitude: number, longitude: number}` objects. This creates a route passing through all the given coordinates. Requires at least 2 points for start and end destinations.
99
+
100
+ #### `waypointIndices`
101
+
102
+ An array of indices representing which of the coordinates is to be considered a waypoint/destination. By default all coordinates are considered waypoints (and show a waypoint indicator), passing this prop allows choosing which of the coordinates are waypoints. At least the first and last element need to be included in the array for a route to render.
103
+
104
+ #### `useRouteMatchingApi`
105
+
106
+ When enabled, uses Mapbox's Map Matching API to generate the route instead of the regular navigation route generation APIs. Enable this when you want a more explicit path determined by the coordinates. [Mapbox Map Matching API](https://docs.mapbox.com/api/navigation/map-matching/)
107
+
108
+ #### `locale`
109
+
110
+ A string representation of a locale/language code that adjusts the Map labels, directions, and voice where possible. By default, uses the devices locale.
111
+
112
+ #### `routeProfile`
113
+
114
+ The profile to use for route generation. `mapbox/driving-traffic` by default. See [here](https://docs.mapbox.com/api/navigation/directions/#routing-profiles) for details. NOTE: For android, you need to omit the `mapbox/` prefix.
115
+
116
+ #### `routeExcludeList`
117
+
118
+ An array of road types and locations to exclude from the route. See the `exclude` param [here](https://docs.mapbox.com/api/navigation/directions/#optional-parameters) for details.
119
+
120
+ #### `mapStyle`
121
+
122
+ The style of the Mapbox map. See [here](https://docs.mapbox.com/api/maps/styles/) for details.
123
+
124
+ #### `mute`
125
+
126
+ Whether the navigation audio is initially muted.
127
+
128
+ #### `vehicleMaxHeight`
129
+
130
+ The maximum height of the vehicle in meters. This is used to avoid routes with height restrictions.
131
+
132
+ #### `vehicleMaxWidth`
133
+
134
+ The maximum width of the vehicle in meters. This is used to avoid routes with width restrictions.
135
+
136
+ #### `customRasterSourceUrl`
137
+
138
+ The URL of a custom raster source to use for the map. Should be a template string with `{x}`, `{y}`, `{z}` placeholders.
139
+ Example: `"https://tile.openstreetmap.org/{z}/{x}/{y}.png"`
140
+
141
+ #### `placeCustomRasterLayerAbove`
142
+
143
+ The ID of the layer above which the custom raster layer should be placed. Useful for controlling the rendering order of map layers.
144
+
145
+ #### `disableAlternativeRoutes`
146
+
147
+ If true, disables calculation and display of alternative routes. By default, alternative routes may be shown if available.
148
+
149
+ #### `onRouteProgressChanged`
150
+
151
+ Called when the user's progress on the route changes.
152
+ Provides an object in the `nativeEvent` of type:
153
+
154
+ ```ts
155
+ {
156
+ distanceRemaining: number;
157
+ distanceTraveled: number;
158
+ durationRemaining: number;
159
+ fractionTraveled: number;
160
+ }
161
+ ```
162
+
163
+ #### `onCancelNavigation`
164
+
165
+ Called when the cancel navigation button is clicked. The library does not handle cancellation automatically, use this callback to handle the cancellation behaviour.
166
+
167
+ #### `onWaypointArrival`
168
+
169
+ Called when arrived at one of the given waypoints.
170
+ On **Android ONLY** provides an object in the `nativeEvent` of type:
171
+
172
+ ```ts
173
+ {
174
+ distanceRemaining: number;
175
+ distanceTraveled: number;
176
+ durationRemaining: number;
177
+ fractionTraveled: number;
178
+ }
179
+ ```
180
+
181
+ #### `onFinalDestinationArrival`
182
+
183
+ Called when arrived at the final destination
184
+
185
+ #### `onRouteChanged`
186
+
187
+ Called when the route changes or reroutes
188
+
189
+ #### `onUserOffRoute`
190
+
191
+ Called when the user goes off route
192
+
193
+ #### `onRoutesLoaded`
194
+
195
+ Called when routes are finished loading.
196
+
197
+ ## Running the example app
198
+
199
+ - Run `yarn` on the root directory
200
+ - `cd example`
201
+ - Run `yarn` on the example app
202
+ - Run the app using `npx expo run:android` / `npx expo run:ios`
203
+
204
+ ## Note on Mapbox version updates (for collaborators)
205
+
206
+ The Android implmentation uses the gradle dependencies and can easily be updated to newer versions.
207
+
208
+ iOS, however, bundles the Mapbox Navigation SDK into the expo module to be used. As of the date of writing this, the Mapbox Navigation SDK v3 does not support cocoapods and only supports Swift package manager. There is no obvious way on how one can include a package from the Swift package manager into an Expo module, so I opted to bundle the `.xcframework` package instead.
209
+
210
+ > [!NOTE]
211
+ > Cocoapods support is apparently comming soon. When that day comes, this process can be made much easier.
212
+
213
+ ### Getting the `.xcframework` files
214
+
215
+ First make sure .netrc is configured with your mapbox credentials.
216
+
217
+ Clone https://github.com/mapbox/mapbox-navigation-ios from the desired version branch.
218
+
219
+ Use this modified `Package.swift` and update the versions according to the cloned branch. Checksum for `navNativeChecksum` will be incorrect. Run `swift build -c release` which will fail and give you the correct checksum to update with. After updating, proceed with steps, do not re-run.
220
+
221
+ ```swift
222
+ // swift-tools-version:5.7
223
+ // The swift-tools-version declares the minimum version of Swift required to build this package.
224
+
225
+ import PackageDescription
226
+
227
+ let (navNativeVersion, navNativeChecksum) = ("324.0.0", "f7c0b81c2092faf60eea32538e630e5b67bb7d032251548a2b38054d0ede90f6")
228
+
229
+ let mapsVersion: Version = "11.11.0"
230
+ let commonVersion: Version = "24.11.0"
231
+
232
+ let mapboxApiDownloads = "https://api.mapbox.com/downloads/v2"
233
+
234
+ let package = Package(
235
+ name: "MapboxNavigation",
236
+ defaultLocalization: "en",
237
+ platforms: [.iOS(.v14)],
238
+ products: [
239
+ .library(
240
+ name: "MapboxNavigationUIKit",
241
+ targets: ["MapboxNavigationUIKit"]
242
+ ),
243
+ .library(
244
+ name: "MapboxNavigationCore",
245
+ targets: ["MapboxNavigationCore"]
246
+ )
247
+ ],
248
+ dependencies: [
249
+ .package(url: "https://github.com/mapbox/mapbox-maps-ios.git", exact: mapsVersion),
250
+ .package(url: "https://github.com/mapbox/mapbox-common-ios.git", exact: commonVersion),
251
+ .package(url: "https://github.com/mapbox/turf-swift.git", exact: "4.0.0")
252
+ ],
253
+ targets: [
254
+ .target(
255
+ name: "MapboxNavigationUIKit",
256
+ dependencies: [
257
+ "MapboxNavigationCore",
258
+ ],
259
+ exclude: ["Info.plist"],
260
+ resources: [
261
+ .copy("Resources/MBXInfo.plist"),
262
+ .copy("Resources/PrivacyInfo.xcprivacy")
263
+ ]
264
+ ),
265
+ .target(name: "_MapboxNavigationHelpers"),
266
+ .target(
267
+ name: "MapboxNavigationCore",
268
+ dependencies: [
269
+ .product(name: "MapboxCommon", package: "mapbox-common-ios"),
270
+ "MapboxNavigationNative",
271
+ "MapboxDirections",
272
+ "_MapboxNavigationHelpers",
273
+ .product(name: "MapboxMaps", package: "mapbox-maps-ios"),
274
+ ],
275
+ resources: [
276
+ .process("Resources")
277
+ ]
278
+ ),
279
+ .target(
280
+ name: "MapboxDirections",
281
+ dependencies: [
282
+ .product(name: "Turf", package: "turf-swift"),
283
+ ]
284
+ ),
285
+ navNativeBinaryTarget(
286
+ name: "MapboxNavigationNative",
287
+ version: navNativeVersion,
288
+ checksum: navNativeChecksum
289
+ ),
290
+ ]
291
+ )
292
+
293
+ private func navNativeBinaryTarget(name: String, version: String, checksum: String) -> Target {
294
+ let url = "\(mapboxApiDownloads)/dash-native/releases/ios/packages/\(version)/MapboxNavigationNative.xcframework.zip"
295
+ return .binaryTarget(name: name, url: url, checksum: checksum)
296
+ }
297
+ ```
298
+
299
+ Clone https://github.com/giginet/Scipio/ **inside** the cloned mapbox-navigation-ios repo. `cd` inside that repo and run `swift build -c release`.
300
+
301
+ While still in the Scipio repo run
302
+
303
+ ```
304
+ swift run -c release scipio create ../ -f \
305
+ --platforms iOS \
306
+ --only-use-versions-from-resolved-file \
307
+ --enable-library-evolution \
308
+ --support-simulators \
309
+ --embed-debug-symbols \
310
+ --verbose
311
+ ```
312
+
313
+ Based on https://github.com/mapbox/mapbox-navigation-ios/issues/4703#issuecomment-2297452009
@@ -0,0 +1,98 @@
1
+ apply plugin: 'com.android.library'
2
+ apply plugin: 'kotlin-android'
3
+ apply plugin: 'maven-publish'
4
+
5
+ group = 'expo.modules.mapboxnavigation'
6
+ version = '0.1.0'
7
+
8
+ buildscript {
9
+ def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
10
+ if (expoModulesCorePlugin.exists()) {
11
+ apply from: expoModulesCorePlugin
12
+ applyKotlinExpoModulesCorePlugin()
13
+ }
14
+
15
+ // Simple helper that allows the root project to override versions declared by this library.
16
+ ext.safeExtGet = { prop, fallback ->
17
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
18
+ }
19
+
20
+ // Ensures backward compatibility
21
+ ext.getKotlinVersion = {
22
+ if (ext.has("kotlinVersion")) {
23
+ ext.kotlinVersion()
24
+ } else {
25
+ ext.safeExtGet("kotlinVersion", "1.8.10")
26
+ }
27
+ }
28
+
29
+ repositories {
30
+ mavenCentral()
31
+ }
32
+
33
+ dependencies {
34
+ classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
35
+ }
36
+ }
37
+
38
+ afterEvaluate {
39
+ publishing {
40
+ publications {
41
+ release(MavenPublication) {
42
+ from components.release
43
+ }
44
+ }
45
+ repositories {
46
+ maven {
47
+ url = mavenLocal().url
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ android {
54
+ compileSdkVersion safeExtGet("compileSdkVersion", 34)
55
+
56
+ def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
57
+ def agpMajorVersion = agpVersion.tokenize('.')[0].toInteger()
58
+
59
+ // AGP 8+ requires Java 17, AGP 7 and below use Java 11
60
+ compileOptions {
61
+ sourceCompatibility = agpMajorVersion >= 8 ? JavaVersion.VERSION_17 : JavaVersion.VERSION_11
62
+ targetCompatibility = agpMajorVersion >= 8 ? JavaVersion.VERSION_17 : JavaVersion.VERSION_11
63
+ }
64
+
65
+ kotlinOptions {
66
+ jvmTarget = agpMajorVersion >= 8 ? "17" : "11"
67
+ }
68
+
69
+ namespace "expo.modules.mapboxnavigation"
70
+ defaultConfig {
71
+ minSdkVersion safeExtGet("minSdkVersion", 21)
72
+ targetSdkVersion safeExtGet("targetSdkVersion", 34)
73
+ versionCode 1
74
+ versionName "0.1.0"
75
+ }
76
+ lintOptions {
77
+ abortOnError false
78
+ }
79
+ publishing {
80
+ singleVariant("release") {
81
+ withSourcesJar()
82
+ }
83
+ }
84
+ }
85
+
86
+ repositories {
87
+ mavenCentral()
88
+ }
89
+
90
+ dependencies {
91
+ def lifecycle_version = "2.9.3"
92
+ implementation project(':expo-modules-core')
93
+ implementation("com.mapbox.navigationcore:android-ndk27:3.11.0")
94
+ implementation ("com.mapbox.navigationcore:ui-components-ndk27:3.11.0")
95
+ implementation "com.google.android.gms:play-services-location:21.0.1"
96
+ implementation "androidx.constraintlayout:constraintlayout:2.2.0-alpha13"
97
+ implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
98
+ }
@@ -0,0 +1,2 @@
1
+ <manifest>
2
+ </manifest>
@@ -0,0 +1,120 @@
1
+ package expo.modules.mapboxnavigation
2
+
3
+ import androidx.lifecycle.LifecycleOwner
4
+ import androidx.lifecycle.lifecycleScope
5
+ import com.mapbox.geojson.Point
6
+ import com.mapbox.navigation.base.options.NavigationOptions
7
+ import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
8
+ import expo.modules.kotlin.modules.Module
9
+ import expo.modules.kotlin.modules.ModuleDefinition
10
+ import kotlinx.coroutines.Dispatchers
11
+ import kotlinx.coroutines.launch
12
+
13
+ class ExpoMapboxNavigationModule : Module() {
14
+ private val activity
15
+ get() = requireNotNull(appContext.activityProvider?.currentActivity)
16
+
17
+ @com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
18
+ override fun definition() = ModuleDefinition {
19
+ Name("ExpoMapboxNavigation")
20
+
21
+ OnActivityEntersForeground {
22
+ (activity as LifecycleOwner).lifecycleScope.launch(Dispatchers.Main) {
23
+ if (!MapboxNavigationApp.isSetup()) {
24
+ MapboxNavigationApp.setup {
25
+ NavigationOptions.Builder(activity.applicationContext).build()
26
+ }
27
+ }
28
+ MapboxNavigationApp.attach(activity as LifecycleOwner)
29
+ }
30
+ }
31
+
32
+ View(ExpoMapboxNavigationView::class) {
33
+ Events(
34
+ "onRouteProgressChanged",
35
+ "onCancelNavigation",
36
+ "onWaypointArrival",
37
+ "onFinalDestinationArrival",
38
+ "onRouteChanged",
39
+ "onUserOffRoute",
40
+ "onRoutesLoaded",
41
+ "onRouteFailedToLoad"
42
+ )
43
+
44
+ Prop("coordinates") { view: ExpoMapboxNavigationView, coordinates: List<Map<String, Any>> ->
45
+ val points = mutableListOf<Point>()
46
+ for (coordinate in coordinates) {
47
+ val longValue = coordinate.get("longitude")
48
+ val latValue = coordinate.get("latitude")
49
+ if (longValue is Double && latValue is Double) {
50
+ points.add(Point.fromLngLat(longValue, latValue))
51
+ }
52
+ }
53
+ view.setCoordinates(points)
54
+ }
55
+
56
+ Prop("vehicleMaxHeight") { view: ExpoMapboxNavigationView, maxHeight: Double? ->
57
+ view.setVehicleMaxHeight(maxHeight)
58
+ }
59
+
60
+ Prop("vehicleMaxWidth") { view: ExpoMapboxNavigationView, maxWidth: Double? ->
61
+ view.setVehicleMaxWidth(maxWidth)
62
+ }
63
+
64
+ Prop("waypointIndices") { view: ExpoMapboxNavigationView, indices: List<Int>? ->
65
+ view.setWaypointIndices(indices)
66
+ }
67
+
68
+ Prop("locale") { view: ExpoMapboxNavigationView, localeStr: String? ->
69
+ view.setLocale(localeStr)
70
+ }
71
+
72
+ Prop("useRouteMatchingApi") { view: ExpoMapboxNavigationView, useRouteMatchingApi: Boolean? ->
73
+ view.setIsUsingRouteMatchingApi(useRouteMatchingApi)
74
+ }
75
+
76
+ Prop("routeProfile") { view: ExpoMapboxNavigationView, profile: String? ->
77
+ view.setRouteProfile(profile)
78
+ }
79
+
80
+ Prop("routeExcludeList") { view: ExpoMapboxNavigationView, excludeList: List<String>? ->
81
+ view.setRouteExcludeList(excludeList)
82
+ }
83
+
84
+ Prop("mapStyle") { view: ExpoMapboxNavigationView, style: String? -> view.setMapStyle(style) }
85
+
86
+ Prop("mute") { view: ExpoMapboxNavigationView, isMuted: Boolean? -> view.setIsMuted(isMuted) }
87
+
88
+ Prop("initialLocation") { view: ExpoMapboxNavigationView, initialLocation: Map<String, Any>?
89
+ ->
90
+ val longValue = initialLocation?.get("longitude")
91
+ val latValue = initialLocation?.get("latitude")
92
+ val zoomValue = initialLocation?.get("zoom")
93
+
94
+ if (longValue is Double && latValue is Double && zoomValue is Double?) {
95
+ view.setInitialLocation(Point.fromLngLat(longValue, latValue), zoomValue)
96
+ }
97
+ }
98
+
99
+ Prop("customRasterSourceUrl") { view: ExpoMapboxNavigationView, url: String? ->
100
+ view.setCustomRasterSourceUrl(url)
101
+ }
102
+
103
+ Prop("placeCustomRasterLayerAbove") { view: ExpoMapboxNavigationView, layerId: String? ->
104
+ view.setPlaceCustomRasterLayerAbove(layerId)
105
+ }
106
+
107
+ Prop("disableAlternativeRoutes") {
108
+ view: ExpoMapboxNavigationView,
109
+ disableAlternativeRoutes: Boolean? ->
110
+ view.setDisableAlternativeRoutes(disableAlternativeRoutes)
111
+ }
112
+
113
+ Prop("followingZoom") { view: ExpoMapboxNavigationView, followingZoom: Double? ->
114
+ view.setFollowingZoom(followingZoom)
115
+ }
116
+
117
+ AsyncFunction("recenterMap") { view: ExpoMapboxNavigationView -> view.recenterMap() }
118
+ }
119
+ }
120
+ }