Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4. //Setting up the Location Manager.
  5. locationManager.delegate = self
  6. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  7. locationManager.requestWhenInUseAuthorization()
  8. locationManager.startUpdatingLocation()
  9.  
  10. //Showing user location on the map as a blue dot.
  11. self.theMap.showsUserLocation = true
  12.  
  13. //Setting the delegate for the map view.
  14. self.theMap.delegate = self
  15. }
  16.  
  17.  
  18. func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  19.  
  20. //Getting the last recorded location.
  21. let location = locations.last
  22. let ceter = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
  23.  
  24. //Setting the region of the map.
  25. let region = MKCoordinateRegion(center: ceter, span: MKCoordinateSpanMake(0.01, 0.01))
  26.  
  27. //Assigning the region to the map.
  28. self.theMap.setRegion(region, animated: true)
  29.  
  30. //Stop updating the location.
  31. self.locationManager.stopUpdatingLocation()
  32.  
  33. }
  34.  
  35.  
  36. func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
  37. //Getting the center coordinate of the screen.
  38. let thecenter = mapView.centerCoordinate
  39.  
  40. //Storing latitude & longitude in seperate variables.
  41. let centerLat = thecenter.latitude
  42. let centerlon = thecenter.longitude
  43.  
  44. //Converting CLLocationCoordinate2D to CLLocation.
  45. let movedMap: CLLocation = CLLocation(latitude: centerLat, longitude: centerlon)
  46.  
  47. //Performing Reverse GeoCoding to retrieve address from Coordinates.
  48. CLGeocoder().reverseGeocodeLocation(movedMap) { (placemarks, error) -> Void in
  49.  
  50. //Checking for error.
  51. if(error != nil) {
  52. print(error)
  53. }else{
  54.  
  55. //Taking the first coordinates among the lot & storing in variable 'p'.
  56. if let p = placemarks?.last {
  57.  
  58. //Unwrapping Optional Strings.
  59. let roadno = p.subThoroughfare ?? ""
  60.  
  61. //Checking if subThoroughfare exists.
  62. if(p.subThoroughfare != nil) {
  63.  
  64. //Unwrapping Optional Strings.
  65. let thoroughfare = p.thoroughfare ?? ""
  66. let subLocality = p.subLocality ?? ""
  67. let locality = p.locality ?? ""
  68. let administrativeArea = p.administrativeArea ?? ""
  69. let postalCode = p.postalCode ?? ""
  70. let country = p.country ?? ""
  71.  
  72. let address = " (roadno) r (thoroughfare) r (subLocality) r (locality) (administrativeArea) (postalCode) r (country)"
  73. print(address)
  74.  
  75. //Assigning the address to the address label on the map.
  76. self.addressLabel.text = " (roadno) r (thoroughfare) r (subLocality) r (locality) (administrativeArea) (postalCode) r (country)"
  77.  
  78. }else{
  79.  
  80. //Unwrapping Optional Strings.
  81. let thoroughfare = p.thoroughfare ?? ""
  82. let subLocality = p.subLocality ?? ""
  83. let locality = p.locality ?? ""
  84. let administrativeArea = p.administrativeArea ?? ""
  85. let postalCode = p.postalCode ?? ""
  86. let country = p.country ?? ""
  87.  
  88. let address = " (roadno) r (thoroughfare) r (subLocality) r (locality) (administrativeArea) (postalCode) r (country)"
  89.  
  90. print(address)
  91. //Assigning the address to the address label on the map.
  92. self.addressLabel.text = " (thoroughfare) r (subLocality) r (locality) (administrativeArea) (postalCode) r (country)"
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99. override func viewDidLoad() {
  100. super.viewDidLoad()
  101.  
  102. let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
  103. let span = MKCoordinateSpanMake(100, 80)
  104. let region = MKCoordinateRegionMake(coordinate, span)
  105. self.theMap.setRegion(region, animated: true)
  106.  
  107.  
  108. //Setting up the Location Manager.
  109. locationManager.delegate = self
  110. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  111. locationManager.requestWhenInUseAuthorization()
  112. locationManager.startUpdatingLocation()
  113.  
  114. //Showing user location on the map as a blue dot.
  115. self.theMap.showsUserLocation = true
  116.  
  117. //Setting the delegate for the map view.
  118. self.theMap.delegate = self
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement