Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import MapKit
  2. import UIKit
  3. import CoreLocation
  4.  
  5. class MapViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate,CLLocationManagerDelegate {
  6.  
  7. @IBOutlet weak var MapView: MKMapView!
  8.  
  9. var searchController:UISearchController!
  10. var annotation:MKAnnotation!
  11. var localSearchRequest:MKLocalSearchRequest!
  12. var localSearch:MKLocalSearch!
  13. var localSearchResponse:MKLocalSearchResponse!
  14. var error:NSError!
  15. var pointAnnotation:MKPointAnnotation!
  16. var pinAnnotationView:MKPinAnnotationView!
  17.  
  18. var coordinates: [[Double]]!
  19. var name:[String]!
  20.  
  21. let regionRadius: CLLocationDistance = 1000
  22.  
  23. @IBAction func showSearchBar(_ sender: Any) {
  24.  
  25. searchController = UISearchController(searchResultsController: nil)
  26. searchController.hidesNavigationBarDuringPresentation = false
  27. self.searchController.searchBar.delegate = self
  28. present(searchController, animated: true, completion: nil)
  29. }
  30.  
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33.  
  34.  
  35. self.MapView.delegate = self
  36.  
  37. var Kusamba = CustomPointAnnotation()
  38. Kusamba.coordinate = CLLocationCoordinate2DMake(-8.550436, 115.481009)
  39. Kusamba.title = "Kusumba"
  40. Kusamba.subtitle = "Village de pêcheur et de paludiers"
  41. Kusamba.imageName = "PetitPtVie"
  42.  
  43. var Goa = CustomPointAnnotation()
  44. Goa.coordinate = CLLocationCoordinate2DMake(-8.551077, 115.474824)
  45. Goa.title = "Goa Lawah"
  46. Goa.subtitle = "La grotte des chauves souris"
  47. Goa.imageName = "PetitPtculture"
  48.  
  49. var Andakasa = CustomPointAnnotation()
  50. Andakasa.coordinate = CLLocationCoordinate2DMake(-8.513608, 115.474698)
  51. Andakasa.title = "Andakasa"
  52. Andakasa.subtitle = "Pura Luhur Andakasa"
  53. Andakasa.imageName = "PetitPtculture"
  54.  
  55. var Padang = CustomPointAnnotation()
  56. Padang.coordinate = CLLocationCoordinate2DMake(-8.530652, 115.509299)
  57. Padang.title = "Padang bai"
  58. Padang.subtitle = "Village portuaire "
  59. Padang.imageName = "PetitPtplage"
  60.  
  61.  
  62. MapView.addAnnotation(Kusamba)
  63. MapView.addAnnotation(Goa)
  64. MapView.addAnnotation(Andakasa)
  65. MapView.addAnnotation(Padang)
  66.  
  67.  
  68. let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -8.670458199999999, longitude: 115.2126293), span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
  69. self.MapView.setRegion(region, animated: true)
  70. }
  71.  
  72. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
  73.  
  74. print("delegate called")
  75.  
  76. if !(annotation is CustomPointAnnotation) {
  77. return nil
  78. }
  79.  
  80. let reuseId = "test"
  81.  
  82. var AnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
  83. if AnnotationView == nil {
  84. AnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
  85. AnnotationView?.canShowCallout = true
  86.  
  87. let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
  88. AnnotationView?.rightCalloutAccessoryView = rightButton as? UIView
  89. }
  90. else {
  91. AnnotationView?.annotation = annotation
  92.  
  93. }
  94.  
  95. func didReceiveMemoryWarning() {
  96. super.didReceiveMemoryWarning()
  97.  
  98. }
  99.  
  100. let CustomPointAnnotation = annotation as! CustomPointAnnotation
  101. AnnotationView?.image = UIImage(named:CustomPointAnnotation.imageName)
  102.  
  103. return AnnotationView
  104. }
  105.  
  106. class CustomPointAnnotation: MKPointAnnotation {
  107. var imageName: String!
  108. }
  109.  
  110. override func didReceiveMemoryWarning() {
  111. super.didReceiveMemoryWarning()
  112. // Dispose of any resources that can be recreated.
  113. }
  114.  
  115.  
  116. func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
  117.  
  118. searchBar.resignFirstResponder()
  119. dismiss(animated: true, completion: nil)
  120. if self.MapView.annotations.count != 0{
  121. annotation = self.MapView.annotations[0]
  122. self.MapView.removeAnnotation(annotation)
  123. }
  124.  
  125. localSearchRequest = MKLocalSearchRequest()
  126. localSearchRequest.naturalLanguageQuery = searchBar.text
  127. localSearch = MKLocalSearch(request: localSearchRequest)
  128. localSearch.start { (localSearchResponse, error) -> Void in
  129.  
  130. if localSearchResponse == nil{
  131. let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
  132. alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
  133. self.present(alertController, animated: true, completion: nil)
  134. return
  135. }
  136.  
  137. self.pointAnnotation = MKPointAnnotation()
  138. self.pointAnnotation.title = searchBar.text
  139. self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
  140.  
  141.  
  142. self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
  143. self.MapView.centerCoordinate = self.pointAnnotation.coordinate
  144. self.MapView.addAnnotation(self.pinAnnotationView.annotation!)
  145. }
  146.  
  147.  
  148. func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
  149.  
  150. if control == view.rightCalloutAccessoryView {
  151.  
  152. if (title == "Kusumba") {
  153. self.performSegue(withIdentifier: "Kusumba", sender: self)
  154.  
  155. } else if (title == "Goa Lawah"){
  156. self.performSegue(withIdentifier: "Goa Lawah", sender: self)
  157.  
  158. } else if (title == "Andakasa"){
  159. self.performSegue(withIdentifier: "Convertisseur", sender: self)
  160. }
  161. }
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement