Skip to content

Instantly share code, notes, and snippets.

@satriawarn
Last active November 15, 2022 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satriawarn/a8ed0d9792fd2c603e275ff303563d84 to your computer and use it in GitHub Desktop.
Save satriawarn/a8ed0d9792fd2c603e275ff303563d84 to your computer and use it in GitHub Desktop.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
@IBOutlet var mapView: MKMapView!
var resultSearchController:UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
//initilize search bar
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.searchController = resultSearchController
resultSearchController?.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
//initialize table view for result of placemark
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
//initialize mapkit
mapView.delegate = self
}
/**
This method used to monitoring whenever user update their locations.
**/
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
. . .
}
/**
This method used to process 2 placemark.
In this method, you can call renderPolyline and showEstimatedTime.
**/
func createEstimate(_ from: CLLocationCoordinate2D, _ to: CLLocationCoordinate2D){
. . .
}
/**
This method will render the polyline and move the camera
**/
func renderPolyline(_ response: MKDirections.Response){
. . .
}
/**
This method used to show the expectedTravelTime.
**/
func showEstimatedTime(_ response: MKDirections.Response){
. . .
}
/**
This method used to modify the draw of polyline between 2 coordinate
**/
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
. . .
}
/**
This method is calling in the first time when application is open.
To check location services is enable or not, if enable the application
can access current location
**/
func checkIfLocationServicesIsEnable(){
if CLLocationManager.locationServicesEnabled(){
locationManager = CLLocationManager()
locationManager!.delegate = self
} else {
. . .
}
}
/**
This method used to determine the authorization location giver from user.
**/
func checkLocationAuthorization(){
guard let locationManager = locationManager else {return}
switch locationManager.authorizationStatus{
case .notDetermined:
. . .
case .restricted:
. . .
case .denied:
. . .
case .authorizedAlways, .authorizedWhenInUse:
. . .
@unknown default:
break
}
}
/**
This method used to handle authorization of location access,
whenever the user open the application,
perimission to location access is not show up again.
**/
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
checkLocationAuthorization()
}
/**
This function is to change the UI of that's maps
**/
@objc func changeMapType(){
. . .
}
/**
This method used to make the expectedTravelTime readable
**/
func secondsToHoursMinutesSeconds(_ seconds: Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
}
extension ViewController: HandleMapSearch {
/**
This method will created pin to the placemark location and move the camera
**/
func dropPinZoomIn(placemark:MKPlacemark){
. . .
}
}
class LocationSearchTable: UITableViewController {
var matchingItems:[MKMapItem] = []
var mapView: MKMapView? = nil
var handleMapSearchDelegate:HandleMapSearch? = nil
}
extension LocationSearchTable: UISearchResultsUpdating{
func updateSearchResults(for searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
. . .
}
}
extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
. . .
return cell
}
}
extension LocationSearchTable {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row].placemark
handleMapSearchDelegate?.dropPinZoomIn(placemark: selectedItem)
dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment