Skip to content

Add search sdk integration example #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Navigation-Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
C346744E263C57610068A65E /* Beta-Query-Parameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = C346744D263C57610068A65E /* Beta-Query-Parameters.swift */; };
C35F1E4D2656967E00EC54CD /* Offline-Regions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35F1E4C2656967E00EC54CD /* Offline-Regions.swift */; };
C3893F1C25F96BDC0058A987 /* Custom-Waypoints.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3893F1B25F96BDC0058A987 /* Custom-Waypoints.swift */; };
C3D9C06D27B59E7000F6C0B3 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D9C06C27B59E7000F6C0B3 /* Search.swift */; };
C52BAA782040D5030086EC6B /* Custom-Destination-Marker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52BAA772040D5030086EC6B /* Custom-Destination-Marker.swift */; };
C58FB85A1FE899B800C4B491 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58FB8591FE899B800C4B491 /* AppDelegate.swift */; };
C58FB8611FE899B800C4B491 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C58FB8601FE899B800C4B491 /* Assets.xcassets */; };
Expand Down Expand Up @@ -94,6 +95,7 @@
C346744D263C57610068A65E /* Beta-Query-Parameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Beta-Query-Parameters.swift"; sourceTree = "<group>"; };
C35F1E4C2656967E00EC54CD /* Offline-Regions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Offline-Regions.swift"; sourceTree = "<group>"; };
C3893F1B25F96BDC0058A987 /* Custom-Waypoints.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Custom-Waypoints.swift"; sourceTree = "<group>"; };
C3D9C06C27B59E7000F6C0B3 /* Search.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
C52BAA772040D5030086EC6B /* Custom-Destination-Marker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Custom-Destination-Marker.swift"; sourceTree = "<group>"; };
C58FB8561FE899B800C4B491 /* Navigation-Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Navigation-Examples.app"; sourceTree = BUILT_PRODUCTS_DIR; };
C58FB8591FE899B800C4B491 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -246,6 +248,7 @@
C58FB8721FE98AEB00C4B491 /* Styled-UI-Elements.swift */,
C58FB8761FE98B0E00C4B491 /* Waypoint-Arrival-Screen.swift */,
8DF510731FEB08F70049DB9C /* Embedded-Navigation.swift */,
C3D9C06C27B59E7000F6C0B3 /* Search.swift */,
8D6D36A620001F0400C2B61B /* EmbeddedExamples.storyboard */,
C5F130A51FEB2D7800463E86 /* Advanced.swift */,
C346744D263C57610068A65E /* Beta-Query-Parameters.swift */,
Expand Down Expand Up @@ -279,7 +282,6 @@
4EF74F60792B221A1A61A3CE /* Pods-Navigation-Examples.debug.xcconfig */,
A8BC0BE26AF9683D3B034741 /* Pods-Navigation-Examples.release.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -334,6 +336,8 @@
dependencies = (
);
name = "Navigation-Examples";
packageProductDependencies = (
);
productName = "Navigation Examples";
productReference = C58FB8561FE899B800C4B491 /* Navigation-Examples.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -369,6 +373,8 @@
Base,
);
mainGroup = C58FB84D1FE899B800C4B491;
packageReferences = (
);
productRefGroup = C58FB8571FE899B800C4B491 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -462,6 +468,7 @@
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxCoreMaps/MapboxCoreMaps.framework/MapboxCoreMaps",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxMobileEvents/MapboxMobileEvents.framework/MapboxMobileEvents",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxNavigationNative/MapboxNavigationNative.framework/MapboxNavigationNative",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxSearch/MapboxSearch.framework/MapboxSearch",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
Expand All @@ -477,6 +484,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxCoreMaps.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxMobileEvents.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxNavigationNative.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxSearch.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -576,6 +584,7 @@
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxCoreMaps/MapboxCoreMaps.framework/MapboxCoreMaps",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxMobileEvents/MapboxMobileEvents.framework/MapboxMobileEvents",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxNavigationNative/MapboxNavigationNative.framework/MapboxNavigationNative",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/MapboxSearch/MapboxSearch.framework/MapboxSearch",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
Expand All @@ -591,6 +600,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxCoreMaps.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxMobileEvents.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxNavigationNative.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxSearch.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -628,6 +638,7 @@
2B521D502409240E00984CF8 /* CustomBottomBannerView.swift in Sources */,
B480E428261E4C4600C16FA0 /* Route-Alerts.swift in Sources */,
C58FB8771FE98B0E00C4B491 /* Waypoint-Arrival-Screen.swift in Sources */,
C3D9C06D27B59E7000F6C0B3 /* Search.swift in Sources */,
C35F1E4D2656967E00EC54CD /* Offline-Regions.swift in Sources */,
C5F130A61FEB2D7800463E86 /* Advanced.swift in Sources */,
C58FB86C1FE89CEC00C4B491 /* ExampleContainerViewController.swift in Sources */,
Expand Down
9 changes: 9 additions & 0 deletions Navigation-Examples/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ let listOfExamples: [NamedController] = [
storyboard: nil,
pushExampleToViewController: true
),
(
name: "Search Integration",
description:"""
Demonstrates how to integrate the Mapbox Search SDK with Mapbox Navigation
""",
controller: SearchViewController.self,
storyboard: nil,
pushExampleToViewController: true
),
(
name: "Basic",
description: "A basic hello world example showing how to create a navigation experience using the fewest lines of code possible.",
Expand Down
103 changes: 103 additions & 0 deletions Navigation-Examples/Examples/Search.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import UIKit
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections
import MapboxMaps
import MapboxSearch

class SearchViewController: UIViewController, NavigationMapViewDelegate, NavigationViewControllerDelegate, UISearchResultsUpdating, UITableViewDataSource {

var navigationMapView: NavigationMapView!
var navigationRouteOptions: NavigationRouteOptions!

let searchEngine = SearchEngine()
var searchController: UISearchController!
var tableView: UITableView!

var searchSuggestions: [SearchSuggestion] = []

var isSearchBarEmpty: Bool {
return searchController.searchBar.text?.isEmpty ?? true
}

// MARK: - UIViewController lifecycle methods

override func viewDidLoad() {
super.viewDidLoad()

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.placeholder = "Where to?"
self.definesPresentationContext = true
searchController.searchBar.sizeToFit()

tableView = UITableView()
tableView.dataSource = self
tableView.tableHeaderView = searchController.searchBar
view.addSubview(tableView)

searchEngine.delegate = self

// view.addSubview(navigationMapView)
// view.setNeedsLayout()
}

// override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
// searchController.searchBar.becomeFirstResponder()
// }

// Override layout lifecycle callback to be able to style the start button.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = searchSuggestions[indexPath.row].name
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchSuggestions.count
}

func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
searchEngine.query = searchText
tableView.reloadData()
searchEngine.reverseGeocoding(options: <#T##ReverseGeocodingOptions#>, completion: <#T##(Result<[SearchResult], SearchError>) -> Void#>)
}
}

// Send search results to Nav SDK
// func handleSearchResultSelection(_ searchResult: SearchResult) {
// let coordinate = searchResult.routablePoints?.first?.point ?? searchResult.coordinate
// let destinationWaypoint = Waypoint(coordinate: coordinate, name: "\(searchResult.name)")
// let navigationRouteOptions = NavigationRouteOptions(waypoints: [userWaypoint, destinationWaypoint])
// Directions.calculateRoutes(navigationRouteOptions) { [weak self (session, result) in
// switch result {
// case .failure(let error):
// print(error)
// case .success(let response):
// // Handle RouteResponse
// }
// }
// }
}

extension SearchViewController: SearchEngineDelegate {
func resultResolved(result: SearchResult, searchEngine: SearchEngine) {
print("FIX ME!")
}

func searchErrorHappened(searchError: SearchError, searchEngine: SearchEngine) {
print("Error during search: \(searchError)")
}

func suggestionsUpdated(suggestions: [SearchSuggestion], searchEngine: SearchEngine) {
// Handle suggestions
searchSuggestions = suggestions
updateSearchResults(for: searchController)
}
}
5 changes: 3 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ install! 'cocoapods', :warn_for_unused_master_specs_repo => false
platform :ios, '11.0'
use_frameworks!

pod 'MapboxCoreNavigation', '~> 2.0'
pod 'MapboxNavigation', '~> 2.0'
pod 'MapboxCoreNavigation', '2.2.0'
pod 'MapboxNavigation', '2.2.0'
pod 'MapboxSearch', '1.0.0-beta.20'

target 'Navigation-Examples' do
end
Expand Down
16 changes: 11 additions & 5 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ PODS:
- MapboxMobileEvents (1.0.6)
- MapboxNavigation (2.2.0):
- MapboxCoreNavigation (= 2.2.0)
- MapboxMaps (= 10.2.0)
- MapboxMaps (~> 10.2)
- MapboxMobileEvents (~> 1.0)
- MapboxSpeech (~> 2.0)
- Solar-dev (~> 3.0)
- MapboxNavigationNative (83.0.0):
- MapboxCommon (~> 21.0)
- MapboxSearch (1.0.0-beta.20):
- MapboxCommon (~> 21.0.0)
- MapboxMobileEvents (~> 1.0.6)
- MapboxSpeech (2.0.0)
- Polyline (5.0.2)
- Solar-dev (3.0.1)
- Turf (2.1.0)

DEPENDENCIES:
- MapboxCoreNavigation (~> 2.0)
- MapboxNavigation (~> 2.0)
- MapboxCoreNavigation (= 2.2.0)
- MapboxNavigation (= 2.2.0)
- MapboxSearch (= 1.0.0-beta.20)

SPEC REPOS:
trunk:
Expand All @@ -42,6 +46,7 @@ SPEC REPOS:
- MapboxMobileEvents
- MapboxNavigation
- MapboxNavigationNative
- MapboxSearch
- MapboxSpeech
- Polyline
- Solar-dev
Expand All @@ -54,13 +59,14 @@ SPEC CHECKSUMS:
MapboxDirections: aaa6f1ae1612692ef3b4211d20d22404ad8ef607
MapboxMaps: 1ed477cbe573a9e740801dfc9c636ef33bbc3f54
MapboxMobileEvents: 14d7ac3ee95b4142c4fec2205dfd48ff453e8871
MapboxNavigation: 77fce7e09f1323a1f10c5ee5d9c238aaee2d7d85
MapboxNavigation: e63908a18a74cc6d629c262ee28c9ab0a370388e
MapboxNavigationNative: a82594cb6fa26c129e2f1a08d1466c3f98cdc999
MapboxSearch: 3544f86d42139789bc2f868c567d627157f45209
MapboxSpeech: e4ed02984444b6373374c72c369edaf045cc490c
Polyline: fce41d72e1146c41c6d081f7656827226f643dff
Solar-dev: 4612dc9878b9fed2667d23b327f1d4e54e16e8d0
Turf: d1220730a00b50a9fb5862c1fb6a5dd698c9e367

PODFILE CHECKSUM: ade747cd70b06f12dd4076e55177eb029b4813b0
PODFILE CHECKSUM: 38d8c2de2a1dfaf4dbb57940a6d54d169f038e8e

COCOAPODS: 1.11.2