Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MapKit
- import UIKit
- import CoreLocation
- class MapViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate,CLLocationManagerDelegate {
- @IBOutlet weak var MapView: MKMapView!
- var searchController:UISearchController!
- var annotation:MKAnnotation!
- var localSearchRequest:MKLocalSearchRequest!
- var localSearch:MKLocalSearch!
- var localSearchResponse:MKLocalSearchResponse!
- var error:NSError!
- var pointAnnotation:MKPointAnnotation!
- var pinAnnotationView:MKPinAnnotationView!
- var coordinates: [[Double]]!
- var name:[String]!
- let regionRadius: CLLocationDistance = 1000
- @IBAction func showSearchBar(_ sender: Any) {
- searchController = UISearchController(searchResultsController: nil)
- searchController.hidesNavigationBarDuringPresentation = false
- self.searchController.searchBar.delegate = self
- present(searchController, animated: true, completion: nil)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- self.MapView.delegate = self
- var Kusamba = CustomPointAnnotation()
- Kusamba.coordinate = CLLocationCoordinate2DMake(-8.550436, 115.481009)
- Kusamba.title = "Kusumba"
- Kusamba.subtitle = "Village de pêcheur et de paludiers"
- Kusamba.imageName = "PetitPtVie"
- var Goa = CustomPointAnnotation()
- Goa.coordinate = CLLocationCoordinate2DMake(-8.551077, 115.474824)
- Goa.title = "Goa Lawah"
- Goa.subtitle = "La grotte des chauves souris"
- Goa.imageName = "PetitPtculture"
- var Andakasa = CustomPointAnnotation()
- Andakasa.coordinate = CLLocationCoordinate2DMake(-8.513608, 115.474698)
- Andakasa.title = "Andakasa"
- Andakasa.subtitle = "Pura Luhur Andakasa"
- Andakasa.imageName = "PetitPtculture"
- var Padang = CustomPointAnnotation()
- Padang.coordinate = CLLocationCoordinate2DMake(-8.530652, 115.509299)
- Padang.title = "Padang bai"
- Padang.subtitle = "Village portuaire "
- Padang.imageName = "PetitPtplage"
- MapView.addAnnotation(Kusamba)
- MapView.addAnnotation(Goa)
- MapView.addAnnotation(Andakasa)
- MapView.addAnnotation(Padang)
- let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -8.670458199999999, longitude: 115.2126293), span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
- self.MapView.setRegion(region, animated: true)
- }
- func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
- print("delegate called")
- if !(annotation is CustomPointAnnotation) {
- return nil
- }
- let reuseId = "test"
- var AnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
- if AnnotationView == nil {
- AnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
- AnnotationView?.canShowCallout = true
- let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
- AnnotationView?.rightCalloutAccessoryView = rightButton as? UIView
- }
- else {
- AnnotationView?.annotation = annotation
- }
- func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- let CustomPointAnnotation = annotation as! CustomPointAnnotation
- AnnotationView?.image = UIImage(named:CustomPointAnnotation.imageName)
- return AnnotationView
- }
- class CustomPointAnnotation: MKPointAnnotation {
- var imageName: String!
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
- searchBar.resignFirstResponder()
- dismiss(animated: true, completion: nil)
- if self.MapView.annotations.count != 0{
- annotation = self.MapView.annotations[0]
- self.MapView.removeAnnotation(annotation)
- }
- localSearchRequest = MKLocalSearchRequest()
- localSearchRequest.naturalLanguageQuery = searchBar.text
- localSearch = MKLocalSearch(request: localSearchRequest)
- localSearch.start { (localSearchResponse, error) -> Void in
- if localSearchResponse == nil{
- let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
- alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
- self.present(alertController, animated: true, completion: nil)
- return
- }
- self.pointAnnotation = MKPointAnnotation()
- self.pointAnnotation.title = searchBar.text
- self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
- self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
- self.MapView.centerCoordinate = self.pointAnnotation.coordinate
- self.MapView.addAnnotation(self.pinAnnotationView.annotation!)
- }
- func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
- if control == view.rightCalloutAccessoryView {
- if (title == "Kusumba") {
- self.performSegue(withIdentifier: "Kusumba", sender: self)
- } else if (title == "Goa Lawah"){
- self.performSegue(withIdentifier: "Goa Lawah", sender: self)
- } else if (title == "Andakasa"){
- self.performSegue(withIdentifier: "Convertisseur", sender: self)
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement