Advertisement
Guest User

ViewController

a guest
Aug 3rd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.28 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // HouseNumbers
  4. //
  5. // Created by BogdanB on 20/07/15.
  6. // Copyright (c) 2015 skobbler. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SKMaps
  11.  
  12.  
  13. class ViewController: UIViewController, SKMapViewDelegate, SKMapVersioningDelegate, SKCalloutViewDelegate, SKPositionerServiceDelegate {
  14. var editUi: EditView!
  15. var topBar: TopBarView!
  16. var mapView: SKMapView!
  17. var fullScreenButton: UIButton!
  18. var fullScreenImage: UIImage!
  19. var leftPickerView: SpinnerView!
  20. var screen: ScreenMode = ScreenMode.FullScreen
  21. var rightPickerView: SpinnerView!
  22. let annotation = SKAnnotation()
  23. let annotation11 = SKAnnotation()
  24. let annotation2 = SKAnnotation()
  25. var currentAnnotation: SKAnnotation!
  26.  
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. getCurrentStreet()
  30. initializeMainViews()
  31. setFullscreenButton()
  32. setPickers()
  33. setMapView()
  34. addSubviews()
  35. setAnnotation()
  36. populateDataArray()
  37. addDirectionsButtonsTargets()
  38. addCancelTargetToEditUI()
  39. addDoneTargeToEditUI()
  40. }
  41.  
  42. func fullScreenButtonPressed(sender: UIButton){
  43. switch screen {
  44.  
  45. case .FullScreen:
  46. UIView.animateWithDuration(0.15, delay: 0.0,
  47. options: UIViewAnimationOptions.BeginFromCurrentState, animations: { self.mapView.frame = CGRectMake(0.0,self.topBar.frame.height, self.view.frame.width , self.view.frame.height - self.topBar.frame.height) }, completion: nil)
  48.  
  49. mapView.settings.panningEnabled = true
  50. mapView.settings.followUserPosition = false
  51. mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
  52. mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
  53. mapView.settings.zoomLimits.mapZoomLimitMin = 0.5
  54. mapView.settings.headingMode = SKHeadingMode.None
  55. var location = SKPositionerService.sharedInstance().currentCoordinate
  56. mapView.animateToLocation(location, withDuration: 0.0)
  57. leftPickerView.removeFromSuperview()
  58. rightPickerView.removeFromSuperview()
  59. screen.toggle()
  60.  
  61. case .Normal:
  62. setMapView()
  63. screen.toggle()
  64. }
  65. }
  66.  
  67. func setMapView(){
  68. UIView.animateWithDuration(0.2, delay: 0.0,
  69. options: UIViewAnimationOptions.BeginFromCurrentState,
  70. animations: { self.mapView.frame = CGRectMake(0.0, self.topBar.frame.height, self.view.frame.width, (self.view.frame.height - self.topBar.frame.height) / 2 ) },
  71. completion: nil)
  72.  
  73. mapView.delegate = self
  74. mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
  75. mapView.mapScaleView.hidden = true
  76. mapView.settings.rotationEnabled = false
  77. mapView.settings.followUserPosition = true
  78. mapView.settings.panningEnabled = false
  79. mapView.settings.headingMode = SKHeadingMode.RotatingMap
  80. mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
  81. mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
  82. view.addSubview(leftPickerView)
  83. view.addSubview(rightPickerView)
  84. }
  85.  
  86. func mapsVersioningManagerLoadedMetadata(versioningManager: SKMapsVersioningManager!) {
  87. println("Loaded metadata");
  88. }
  89.  
  90. func getCurrentStreet() {
  91. var location = SKPositionerService.sharedInstance().currentCoordinate
  92. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(location)
  93. var streetName = searchObject.name
  94. currentStreetName = streetName
  95. }
  96.  
  97. func setPickers() {
  98. leftPickerView = SpinnerView(frame: CGRectMake(0, topBar.frame.height + mapView.frame.height, self.view.frame.width / 2 - 1, self.view.frame.height / 2 - self.view.frame.height / 17.14285 ))
  99. leftPickerView.titleLabel.text = " Left Side \n House number: "
  100. leftPickerView.line.backgroundColor = UIColor.cyanColor()
  101. leftPickerView.addButton.backgroundColor = UIColor.cyanColor()
  102. leftPickerView.addButton.addTarget(self, action: "addToLeft", forControlEvents: UIControlEvents.TouchUpInside)
  103. leftPickerView.numberLabel.textColor = UIColor.cyanColor()
  104.  
  105. rightPickerView = SpinnerView(frame: CGRectMake(view.frame.width / 2 + 1, topBar.frame.height + mapView.frame.height, self.view.frame.width / 2 - 1, self.view.frame.height / 2 - self.view.frame.height / 17.14285 ))
  106. rightPickerView.titleLabel.text = " Right Side \n House number: "
  107. rightPickerView.line.backgroundColor = UIColor.orangeColor()
  108. rightPickerView.addButton.backgroundColor = UIColor.orangeColor()
  109. rightPickerView.addButton.addTarget(self, action: "addToRight", forControlEvents: UIControlEvents.TouchUpInside)
  110. rightPickerView.numberLabel.textColor = UIColor.orangeColor()
  111.  
  112. }
  113.  
  114. func setFullscreenButton() {
  115. fullScreenImage = UIImage(named: "fullscreen.png")
  116. fullScreenButton = UIButton(frame: CGRectMake(mapView.frame.width - 22, 1, 20, 20))
  117. fullScreenButton.setImage(fullScreenImage, forState: UIControlState.Normal)
  118. fullScreenButton.addTarget(self, action: "fullScreenButtonPressed:", forControlEvents:UIControlEvents.TouchUpInside)
  119.  
  120. }
  121.  
  122. func addSubviews() {
  123. view.addSubview(leftPickerView)
  124. view.addSubview(rightPickerView)
  125. self.mapView.addSubview(fullScreenButton)
  126. self.view.addSubview(mapView)
  127. self.view.addSubview(topBar)
  128. }
  129.  
  130. func initializeMainViews() {
  131. topBar = TopBarView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height / 8.89))
  132. editUi = EditView(frame: CGRect(x: 0, y: self.view.frame.height - self.view.frame.height / 2.2533, width: self.view.frame.width, height: self.view.frame.height / 2.2533))
  133. mapView = SKMapView(frame: CGRectMake(0, topBar.frame.height, self.view.frame.width, (self.view.frame.height - topBar.frame.height) / 2 ))
  134. }
  135.  
  136. func setAnnotation() {
  137. annotation.identifier = 10
  138. annotation.annotationType = SKAnnotationType.Red
  139. annotation.location = CLLocationCoordinate2DMake(46.7740, 23.5924)
  140.  
  141. let animationSettings = SKAnimationSettings()
  142. mapView.addAnnotation(annotation, withAnimationSettings: nil)
  143. }
  144.  
  145. func mapView(mapView: SKMapView!, didSelectAnnotation annotation: SKAnnotation!) {
  146. if(screen != ScreenMode.FullScreen) {
  147. currentAnnotation = annotation
  148. var region = SKCoordinateRegion(center: annotation.location, zoomLevel: 19)
  149. mapView.visibleRegion = region
  150.  
  151. UIView.animateWithDuration(0.2, delay: 0.0,
  152. options: UIViewAnimationOptions.BeginFromCurrentState,
  153. animations: { self.mapView.frame = CGRectMake(0.0, self.topBar.frame.height, self.view.frame.width, (self.view.frame.height - self.topBar.frame.height) / 2 ) },
  154. completion: nil)
  155.  
  156. mapView.delegate = self
  157. mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
  158. mapView.mapScaleView.hidden = true
  159. mapView.settings.rotationEnabled = false
  160. mapView.settings.followUserPosition = false
  161. mapView.settings.panningEnabled = false
  162. mapView.settings.zoomLimits.mapZoomLimitMax = 19
  163. mapView.settings.zoomLimits.mapZoomLimitMin = 19
  164. view.addSubview(leftPickerView)
  165. view.addSubview(rightPickerView)
  166. screen.toggle()
  167.  
  168. rightPickerView.removeFromSuperview()
  169. leftPickerView.removeFromSuperview()
  170. topBar.menuButton.removeFromSuperview()
  171. topBar.numberOfSavedInstances.removeFromSuperview()
  172. fullScreenButton.removeFromSuperview()
  173.  
  174. houseNumberAtTopBar = UILabel(frame: CGRectMake(topBar.frame.size.width - topBar.frame.width / 8.5227, topBar.frame.height / 2, topBar.frame.width / 8.5227, topBar.frame.height / 2.7))
  175. houseNumberAtTopBar.textAlignment = NSTextAlignment.Center
  176. houseNumberAtTopBar.adjustsFontSizeToFitWidth = true
  177.  
  178. for i in 0 ... dataArray.count - 1 {
  179. if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
  180. houseNumberAtTopBar.text = dataArray[i].houseNumber
  181. }
  182. }
  183.  
  184. topBar.addSubview(houseNumberAtTopBar)
  185.  
  186. self.view.addSubview(editUi)
  187. }
  188. }
  189.  
  190. func addCancelTargetToEditUI() {
  191. editUi.cancel.addTarget(self, action: "cancelEdit", forControlEvents: UIControlEvents.TouchDown)
  192.  
  193. }
  194.  
  195. func addDoneTargeToEditUI() {
  196. editUi.done.addTarget(self, action: "doneEdit", forControlEvents: UIControlEvents.TouchDown)
  197. }
  198.  
  199. func cancelEdit() {
  200. editUi.removeFromSuperview()
  201. houseNumberAtTopBar.removeFromSuperview()
  202.  
  203. self.view.addSubview(leftPickerView)
  204. self.view.addSubview(rightPickerView)
  205. mapView.addSubview(fullScreenButton)
  206. topBar.addSubview(topBar.menuButton)
  207. topBar.addSubview(topBar.numberOfSavedInstances)
  208.  
  209. mapView.settings.followUserPosition = true
  210. mapView.settings.headingMode = SKHeadingMode.RotatingMap
  211. mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
  212. mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
  213. mapView.animateToZoomLevel(17)
  214. }
  215.  
  216. func doneEdit() {
  217. //TO DO: Update database with current annotation
  218.  
  219. editUi.removeFromSuperview()
  220. houseNumberAtTopBar.removeFromSuperview()
  221.  
  222. self.view.addSubview(leftPickerView)
  223. self.view.addSubview(rightPickerView)
  224. mapView.addSubview(fullScreenButton)
  225. topBar.addSubview(topBar.menuButton)
  226. topBar.addSubview(topBar.numberOfSavedInstances)
  227.  
  228. mapView.settings.followUserPosition = true
  229. mapView.settings.headingMode = SKHeadingMode.RotatingMap
  230. mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
  231. mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
  232. mapView.animateToZoomLevel(17)
  233.  
  234. for i in 0 ... dataArray.count - 1 {
  235. println("\(dataArray[i].street)")
  236.  
  237. topBar.numberOfSavedInstances.text = String(dataArray.count)
  238. }
  239. }
  240.  
  241. func addDirectionsButtonsTargets() {
  242. editUi.up.addTarget(self, action: "upFunc", forControlEvents: UIControlEvents.TouchUpInside)
  243. editUi.down.addTarget(self, action: "downFunc", forControlEvents: UIControlEvents.TouchUpInside)
  244. editUi.right.addTarget(self, action: "rightFunc", forControlEvents: UIControlEvents.TouchUpInside)
  245. editUi.left.addTarget(self, action: "leftFunc", forControlEvents: UIControlEvents.TouchUpInside)
  246. }
  247.  
  248. func upFunc() {
  249. for i in 0 ... dataArray.count - 1 {
  250. if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
  251.  
  252. var aux: CLLocationCoordinate2D
  253. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
  254. var streetName = searchObject.name
  255.  
  256. aux = currentAnnotation.location
  257. aux.latitude = aux.latitude + 0.00005
  258. currentAnnotation.location = aux
  259. dataArray[i].location = aux
  260. dataArray[i].street = streetName
  261. mapView.animateToZoomLevel(18)
  262. mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
  263.  
  264. var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
  265. mapView.visibleRegion = region
  266. mapView.animateToBearing(0.0)
  267. }
  268. }
  269. }
  270.  
  271. func downFunc() {
  272. for i in 0 ... dataArray.count - 1 {
  273. if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
  274.  
  275. var aux: CLLocationCoordinate2D
  276. aux = currentAnnotation.location
  277. aux.latitude = aux.latitude - 0.00005
  278. currentAnnotation.location = aux
  279. dataArray[i].location = aux
  280. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
  281. var streetName = searchObject.name
  282. dataArray[i].street = streetName
  283. mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
  284. var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
  285. mapView.visibleRegion = region
  286. mapView.animateToBearing(0.0)
  287. }
  288. }
  289. }
  290.  
  291. func rightFunc() {
  292. for i in 0 ... dataArray.count - 1 {
  293. if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
  294.  
  295. var aux: CLLocationCoordinate2D
  296. aux = currentAnnotation.location
  297. aux.longitude = aux.longitude + 0.00005
  298. currentAnnotation.location = aux
  299. dataArray[i].location = aux
  300. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
  301. var streetName = searchObject.name
  302. dataArray[i].street = streetName
  303. mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
  304. var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
  305. mapView.visibleRegion = region
  306. mapView.animateToBearing(0.0)
  307. }
  308. }
  309. }
  310.  
  311. func leftFunc() {
  312. for i in 0 ... dataArray.count - 1 {
  313. if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
  314.  
  315. var aux: CLLocationCoordinate2D
  316. aux = currentAnnotation.location
  317. aux.longitude = aux.longitude - 0.00005
  318. currentAnnotation.location = aux
  319. dataArray[i].location = aux
  320. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
  321. var streetName = searchObject.name
  322. dataArray[i].street = streetName
  323. mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
  324. var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
  325. mapView.visibleRegion = region
  326. mapView.animateToBearing(0.0)
  327. }
  328. }
  329. }
  330.  
  331. func populateDataArray() {
  332. var house1 = HouseNumber()
  333. var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(house1.location)
  334. var streetName = searchObject.name
  335.  
  336. house1.id = 1
  337. house1.annotationId = 10
  338. house1.houseNumber = "22"
  339. house1.location = CLLocationCoordinate2D(latitude: 46.7720, longitude: 23.5914)
  340. house1.street = streetName
  341. dataArray.append(house1)
  342.  
  343. annotation11.identifier = Int32(house1.annotationId)
  344. annotation11.annotationType = SKAnnotationType.Red
  345. annotation11.location = house1.location
  346.  
  347. let animationSettings = SKAnimationSettings()
  348. mapView.addAnnotation(annotation11, withAnimationSettings: animationSettings)
  349. topBar.numberOfSavedInstances.text = String(dataArray.count)
  350.  
  351. }
  352.  
  353. func addToLeft() {
  354. println("I think you want to add a house to the left.")
  355. }
  356.  
  357. func addToRight() {
  358. println("I think you want to add a house to the right.")
  359. }
  360.  
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement