Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // HouseNumbers
- //
- // Created by BogdanB on 20/07/15.
- // Copyright (c) 2015 skobbler. All rights reserved.
- //
- import UIKit
- import SKMaps
- class ViewController: UIViewController, SKMapViewDelegate, SKMapVersioningDelegate, SKCalloutViewDelegate, SKPositionerServiceDelegate {
- var editUi: EditView!
- var topBar: TopBarView!
- var mapView: SKMapView!
- var fullScreenButton: UIButton!
- var fullScreenImage: UIImage!
- var leftPickerView: SpinnerView!
- var screen: ScreenMode = ScreenMode.FullScreen
- var rightPickerView: SpinnerView!
- let annotation = SKAnnotation()
- let annotation11 = SKAnnotation()
- let annotation2 = SKAnnotation()
- var currentAnnotation: SKAnnotation!
- override func viewDidLoad() {
- super.viewDidLoad()
- getCurrentStreet()
- initializeMainViews()
- setFullscreenButton()
- setPickers()
- setMapView()
- addSubviews()
- setAnnotation()
- populateDataArray()
- addDirectionsButtonsTargets()
- addCancelTargetToEditUI()
- addDoneTargeToEditUI()
- }
- func fullScreenButtonPressed(sender: UIButton){
- switch screen {
- case .FullScreen:
- UIView.animateWithDuration(0.15, delay: 0.0,
- 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)
- mapView.settings.panningEnabled = true
- mapView.settings.followUserPosition = false
- mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
- mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
- mapView.settings.zoomLimits.mapZoomLimitMin = 0.5
- mapView.settings.headingMode = SKHeadingMode.None
- var location = SKPositionerService.sharedInstance().currentCoordinate
- mapView.animateToLocation(location, withDuration: 0.0)
- leftPickerView.removeFromSuperview()
- rightPickerView.removeFromSuperview()
- screen.toggle()
- case .Normal:
- setMapView()
- screen.toggle()
- }
- }
- func setMapView(){
- UIView.animateWithDuration(0.2, delay: 0.0,
- 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) / 2 ) },
- completion: nil)
- mapView.delegate = self
- mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
- mapView.mapScaleView.hidden = true
- mapView.settings.rotationEnabled = false
- mapView.settings.followUserPosition = true
- mapView.settings.panningEnabled = false
- mapView.settings.headingMode = SKHeadingMode.RotatingMap
- mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
- mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
- view.addSubview(leftPickerView)
- view.addSubview(rightPickerView)
- }
- func mapsVersioningManagerLoadedMetadata(versioningManager: SKMapsVersioningManager!) {
- println("Loaded metadata");
- }
- func getCurrentStreet() {
- var location = SKPositionerService.sharedInstance().currentCoordinate
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(location)
- var streetName = searchObject.name
- currentStreetName = streetName
- }
- func setPickers() {
- 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 ))
- leftPickerView.titleLabel.text = " Left Side \n House number: "
- leftPickerView.line.backgroundColor = UIColor.cyanColor()
- leftPickerView.addButton.backgroundColor = UIColor.cyanColor()
- leftPickerView.addButton.addTarget(self, action: "addToLeft", forControlEvents: UIControlEvents.TouchUpInside)
- leftPickerView.numberLabel.textColor = UIColor.cyanColor()
- 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 ))
- rightPickerView.titleLabel.text = " Right Side \n House number: "
- rightPickerView.line.backgroundColor = UIColor.orangeColor()
- rightPickerView.addButton.backgroundColor = UIColor.orangeColor()
- rightPickerView.addButton.addTarget(self, action: "addToRight", forControlEvents: UIControlEvents.TouchUpInside)
- rightPickerView.numberLabel.textColor = UIColor.orangeColor()
- }
- func setFullscreenButton() {
- fullScreenImage = UIImage(named: "fullscreen.png")
- fullScreenButton = UIButton(frame: CGRectMake(mapView.frame.width - 22, 1, 20, 20))
- fullScreenButton.setImage(fullScreenImage, forState: UIControlState.Normal)
- fullScreenButton.addTarget(self, action: "fullScreenButtonPressed:", forControlEvents:UIControlEvents.TouchUpInside)
- }
- func addSubviews() {
- view.addSubview(leftPickerView)
- view.addSubview(rightPickerView)
- self.mapView.addSubview(fullScreenButton)
- self.view.addSubview(mapView)
- self.view.addSubview(topBar)
- }
- func initializeMainViews() {
- topBar = TopBarView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height / 8.89))
- 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))
- mapView = SKMapView(frame: CGRectMake(0, topBar.frame.height, self.view.frame.width, (self.view.frame.height - topBar.frame.height) / 2 ))
- }
- func setAnnotation() {
- annotation.identifier = 10
- annotation.annotationType = SKAnnotationType.Red
- annotation.location = CLLocationCoordinate2DMake(46.7740, 23.5924)
- let animationSettings = SKAnimationSettings()
- mapView.addAnnotation(annotation, withAnimationSettings: nil)
- }
- func mapView(mapView: SKMapView!, didSelectAnnotation annotation: SKAnnotation!) {
- if(screen != ScreenMode.FullScreen) {
- currentAnnotation = annotation
- var region = SKCoordinateRegion(center: annotation.location, zoomLevel: 19)
- mapView.visibleRegion = region
- UIView.animateWithDuration(0.2, delay: 0.0,
- 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) / 2 ) },
- completion: nil)
- mapView.delegate = self
- mapView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
- mapView.mapScaleView.hidden = true
- mapView.settings.rotationEnabled = false
- mapView.settings.followUserPosition = false
- mapView.settings.panningEnabled = false
- mapView.settings.zoomLimits.mapZoomLimitMax = 19
- mapView.settings.zoomLimits.mapZoomLimitMin = 19
- view.addSubview(leftPickerView)
- view.addSubview(rightPickerView)
- screen.toggle()
- rightPickerView.removeFromSuperview()
- leftPickerView.removeFromSuperview()
- topBar.menuButton.removeFromSuperview()
- topBar.numberOfSavedInstances.removeFromSuperview()
- fullScreenButton.removeFromSuperview()
- 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))
- houseNumberAtTopBar.textAlignment = NSTextAlignment.Center
- houseNumberAtTopBar.adjustsFontSizeToFitWidth = true
- for i in 0 ... dataArray.count - 1 {
- if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
- houseNumberAtTopBar.text = dataArray[i].houseNumber
- }
- }
- topBar.addSubview(houseNumberAtTopBar)
- self.view.addSubview(editUi)
- }
- }
- func addCancelTargetToEditUI() {
- editUi.cancel.addTarget(self, action: "cancelEdit", forControlEvents: UIControlEvents.TouchDown)
- }
- func addDoneTargeToEditUI() {
- editUi.done.addTarget(self, action: "doneEdit", forControlEvents: UIControlEvents.TouchDown)
- }
- func cancelEdit() {
- editUi.removeFromSuperview()
- houseNumberAtTopBar.removeFromSuperview()
- self.view.addSubview(leftPickerView)
- self.view.addSubview(rightPickerView)
- mapView.addSubview(fullScreenButton)
- topBar.addSubview(topBar.menuButton)
- topBar.addSubview(topBar.numberOfSavedInstances)
- mapView.settings.followUserPosition = true
- mapView.settings.headingMode = SKHeadingMode.RotatingMap
- mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
- mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
- mapView.animateToZoomLevel(17)
- }
- func doneEdit() {
- //TO DO: Update database with current annotation
- editUi.removeFromSuperview()
- houseNumberAtTopBar.removeFromSuperview()
- self.view.addSubview(leftPickerView)
- self.view.addSubview(rightPickerView)
- mapView.addSubview(fullScreenButton)
- topBar.addSubview(topBar.menuButton)
- topBar.addSubview(topBar.numberOfSavedInstances)
- mapView.settings.followUserPosition = true
- mapView.settings.headingMode = SKHeadingMode.RotatingMap
- mapView.settings.zoomLimits.mapZoomLimitMax = 18.5
- mapView.settings.zoomLimits.mapZoomLimitMin = 17.0
- mapView.animateToZoomLevel(17)
- for i in 0 ... dataArray.count - 1 {
- println("\(dataArray[i].street)")
- topBar.numberOfSavedInstances.text = String(dataArray.count)
- }
- }
- func addDirectionsButtonsTargets() {
- editUi.up.addTarget(self, action: "upFunc", forControlEvents: UIControlEvents.TouchUpInside)
- editUi.down.addTarget(self, action: "downFunc", forControlEvents: UIControlEvents.TouchUpInside)
- editUi.right.addTarget(self, action: "rightFunc", forControlEvents: UIControlEvents.TouchUpInside)
- editUi.left.addTarget(self, action: "leftFunc", forControlEvents: UIControlEvents.TouchUpInside)
- }
- func upFunc() {
- for i in 0 ... dataArray.count - 1 {
- if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
- var aux: CLLocationCoordinate2D
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
- var streetName = searchObject.name
- aux = currentAnnotation.location
- aux.latitude = aux.latitude + 0.00005
- currentAnnotation.location = aux
- dataArray[i].location = aux
- dataArray[i].street = streetName
- mapView.animateToZoomLevel(18)
- mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
- var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
- mapView.visibleRegion = region
- mapView.animateToBearing(0.0)
- }
- }
- }
- func downFunc() {
- for i in 0 ... dataArray.count - 1 {
- if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
- var aux: CLLocationCoordinate2D
- aux = currentAnnotation.location
- aux.latitude = aux.latitude - 0.00005
- currentAnnotation.location = aux
- dataArray[i].location = aux
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
- var streetName = searchObject.name
- dataArray[i].street = streetName
- mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
- var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
- mapView.visibleRegion = region
- mapView.animateToBearing(0.0)
- }
- }
- }
- func rightFunc() {
- for i in 0 ... dataArray.count - 1 {
- if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
- var aux: CLLocationCoordinate2D
- aux = currentAnnotation.location
- aux.longitude = aux.longitude + 0.00005
- currentAnnotation.location = aux
- dataArray[i].location = aux
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
- var streetName = searchObject.name
- dataArray[i].street = streetName
- mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
- var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
- mapView.visibleRegion = region
- mapView.animateToBearing(0.0)
- }
- }
- }
- func leftFunc() {
- for i in 0 ... dataArray.count - 1 {
- if(dataArray[i].annotationId == Int(currentAnnotation.identifier)) {
- var aux: CLLocationCoordinate2D
- aux = currentAnnotation.location
- aux.longitude = aux.longitude - 0.00005
- currentAnnotation.location = aux
- dataArray[i].location = aux
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(dataArray[i].location)
- var streetName = searchObject.name
- dataArray[i].street = streetName
- mapView.addAnnotation(currentAnnotation, withAnimationSettings: nil)
- var region = SKCoordinateRegion(center: currentAnnotation.location, zoomLevel: 18)
- mapView.visibleRegion = region
- mapView.animateToBearing(0.0)
- }
- }
- }
- func populateDataArray() {
- var house1 = HouseNumber()
- var searchObject: SKSearchResult = SKReverseGeocoderService.sharedInstance().reverseGeocodeLocation(house1.location)
- var streetName = searchObject.name
- house1.id = 1
- house1.annotationId = 10
- house1.houseNumber = "22"
- house1.location = CLLocationCoordinate2D(latitude: 46.7720, longitude: 23.5914)
- house1.street = streetName
- dataArray.append(house1)
- annotation11.identifier = Int32(house1.annotationId)
- annotation11.annotationType = SKAnnotationType.Red
- annotation11.location = house1.location
- let animationSettings = SKAnimationSettings()
- mapView.addAnnotation(annotation11, withAnimationSettings: animationSettings)
- topBar.numberOfSavedInstances.text = String(dataArray.count)
- }
- func addToLeft() {
- println("I think you want to add a house to the left.")
- }
- func addToRight() {
- println("I think you want to add a house to the right.")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement