-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewController.swift
103 lines (69 loc) · 3.32 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// ViewController.swift
// AppleMapKit
//
// Created by Adarsh Sinha on 23/03/18.
// Copyright © 2018 Adarsh Sinha. All rights reserved.
//
import UIKit
import MapKit
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var myMapView: MKMapView!
@IBAction func searchButton(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
present(searchController, animated: true, completion: nil)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
UIApplication.shared.beginIgnoringInteractionEvents()
//- Activity Indicator
let activityIndicator = UIActivityIndicatorView()
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
self.view.addSubview(activityIndicator)
//- Resign Search Bar i.e. Hide Search Bar
searchBar.resignFirstResponder()
dismiss(animated: true, completion: nil)
//- Creating a Search request
let searchRequest = MKLocalSearchRequest()
searchRequest.naturalLanguageQuery = searchBar.text
let activeSearch = MKLocalSearch(request: searchRequest)
activeSearch.start { (response, error) in
activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
if response == nil
{
print("Erorr in the search")
}
else {
//- Remove Annotations
// For each search, all annotations will be removed from the map
let annotations = self.myMapView.annotations
self.myMapView.removeAnnotations(annotations)
// Getting Data
let latitude = response?.boundingRegion.center.latitude
let longitude = response?.boundingRegion.center.longitude
//- Creating Annotations
let annotation = MKPointAnnotation()
annotation.title = searchBar.text
annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)
self.myMapView.addAnnotation(annotation)
//- Zooming in
let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!)
let span = MKCoordinateSpan( latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegionMake(coordinate, span)
self.myMapView.setRegion(region, animated: true)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}