I’ve created search bar with desk view. What I’m making an attempt to attain now’s when the desk view didSelectRowAt
perform known as , I need to navigate into map view and present the pin into map view based mostly on the latitude
and longitude
is handed type desk view didSelectRowAt
perform. I’ve created delegate into map view.
protocol HandleMapSearch: AnyObject {
func dropPinZoomIn(placemark: MKPlacemark)
}
I’m making an attempt to name it type preliminary view controller at didSelectRowAt
like this manner .
self.handleMapSearch?.dropPinZoomIn(placemark: (response?.mapItems[0].placemark)!)
However I’m going through difficulties to move it into map view controller. At present I’m spending some hardcoded worth into map view controller. However I’m not certain How can I move the latitude
and longitude
type didSelectRowAt
perform and present the pin into map view controller.
Right here is the preliminary view controller code.
class ViewController: UIViewController, UISearchBarDelegate, MKLocalSearchCompleterDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var searchResultsTable: UITableView!
var searchCompleter = MKLocalSearchCompleter()
var searchResults = [MKLocalSearchCompletion]()
weak var handleMapSearch: HandleMapSearch?
override func viewDidLoad() {
tremendous.viewDidLoad()
searchCompleter.delegate = self
searchBar?.delegate = self
searchResultsTable?.delegate = self
searchResultsTable?.dataSource = self
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCompleter.queryFragment = searchText
}
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
searchResults = completer.outcomes
searchResultsTable.reloadData()
}
func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
// Error
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return searchResults.depend
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let searchResult = searchResults[indexPath.row]
let cell = UITableViewCell(type: .subtitle, reuseIdentifier: nil)
cell.textLabel?.textual content = searchResult.title
cell.detailTextLabel?.textual content = searchResult.subtitle
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let outcome = searchResults[indexPath.row]
let searchRequest = MKLocalSearch.Request(completion: outcome)
let search = MKLocalSearch(request: searchRequest)
search.begin { (response, error) in
guard let coordinate = response?.mapItems[0].placemark.coordinate else { return }
guard let identify = response?.mapItems[0].identify else { return }
let lat = coordinate.latitude
let lon = coordinate.longitude
self.handleMapSearch?.dropPinZoomIn(placemark: (response?.mapItems[0].placemark)!)
let vc = UIStoryboard.init(identify: "Primary", bundle: Bundle.major).instantiateViewController(withIdentifier: "MapViewController") as! MapViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
Right here is my map view controller.
import UIKit
import MapKit
import Contacts
protocol HandleMapSearch: AnyObject {
func dropPinZoomIn(placemark: MKPlacemark)
}
class MapViewController: UIViewController {
@IBOutlet weak var myMapView: MKMapView!
weak var handleMapSearch: HandleMapSearch?
let coords = CLLocationCoordinate2DMake(51.5083, -0.1384)
let handle = [CNPostalAddressStreetKey: "181 Piccadilly, St. James's", CNPostalAddressCityKey: "London", CNPostalAddressPostalCodeKey: "W1A 1ER", CNPostalAddressISOCountryCodeKey: "GB"]
override func viewDidLoad() {
tremendous.viewDidLoad()
let place = MKPlacemark(coordinate: coords, addressDictionary: handle)
dropPinZoomIn(placemark: place)
}
}
extension MapViewController: HandleMapSearch {
func dropPinZoomIn(placemark: MKPlacemark) {
// clear current pins
myMapView.removeAnnotations(myMapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
annotation.title = placemark.identify
if let metropolis = placemark.locality, let state = placemark.administrativeArea {
annotation.subtitle = "(metropolis) (state)"
}
myMapView.addAnnotation(annotation)
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let area = MKCoordinateRegion(heart: placemark.coordinate, span: span)
myMapView.setRegion(area, animated: true)
}
}