Tuesday 27 June 2017

MapKit : Reverse Geocoding (Detail related to location)



STEP -1 : Add 'Map Kit View' to Main.Storyboard

Goto 'Show Object Library' -> Search and add  'Map Kit View' & one Label.

STEP - 2 : ViewController.swift


//
//  ViewController.swift
//  UserLocation
//
//  Created by Admin on 04/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!
    @IBOutlet weak var label: UILabel!
    
    let manager = CLLocationManager()
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)
        
        print(location.altitude)
        print(location.speed)
        
        self.map.showsUserLocation = true
        CLGeocoder().reverseGeocodeLocation(location) { (placemark, error) in
            if error != nil{
                print("THERE WAS AN ERROR!")
            }else{
                
                if let place = placemark?[0]
                {
                    self.label.text = place.country
                }
            }
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


Shake Gesture



STEP-1 : Add 'Image View' to Main.Storyboard on whole screen

STEP-2 : Add 2 or more image to Assets.xcassets folder.

STEP-3 : ViewController.swift


//
//  ViewController.swift
//  ShakeGesture
//
//  Created by Admin on 27/06/17.
//  Copyright © 2017 Admin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var imageView: UIImageView!
    
    let images = ["image01", "image02", "image03"]
    var currentimage = 0;
    
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        
        if event?.subtype == UIEventSubtype.motionShake
        {
            print("Shaking")
            imageView.image = UIImage(named: images[currentimage]+".jpg")
            if (currentimage == images.count-1)
            {
                currentimage = 0
            }else
            {
                currentimage += 1
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


STORE PERMANENT DATA USING UserDefaults( like android shared pref)



STEP-1 : Add three view (TextField, Button, Label) to Main.Storyboard.

STEP-2 : Make reference of view to CiewController.swift
           
TextField. --> input. (@IBOutlet)
Button      -->. Action (@IBAction)
Label.      -->. output (@IBOutlet)


STEP-3 : ViewController.swift


//
//  ViewController.swift
//  StoringData
//
//  Created by Admin on 27/06/17.
//  Copyright © 2017 Admin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var input: UITextField!
    @IBOutlet weak var output: UILabel!
    
    @IBAction func action(_ sender: Any) {
        
        output.text = input.text
        UserDefaults.standard.set(input.text, forKey: "myName")
        input.text = ""
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        
        if let x = UserDefaults.standard.object(forKey: "myName") as? String
        {
            output.text = x
        }
    }


}


Sunday 25 June 2017

MapKit : User Location, Speed, Altitude

STEP -1 : Add 'Map Kit View' to Main.Storyboard

Goto 'Show Object Library' -> Search 'Map Kit View'.

STEP - 2 : ViewController.swift

//
//  ViewController.swift
//  UserLocation
//
//  Created by Admin on 04/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!
    
    let manager = CLLocationManager()
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)
        
        print(location.altitude)
        print(location.speed)
        
        self.map.showsUserLocation = true
        
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


OUTPUT
======

*User Location show in map.
**Speed and Altitude show in debug console.

Mapkit : Place Annotation(Marker) to Map


STEP -1 : Add 'Map Kit View' to Main.Storyboard

Goto 'Show Object Library' -> Search 'Map Kit View'.

STEP - 2 : ViewController.swift

//
//  ViewController.swift
//  MapTwo
//
//  Created by Admin on 04/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit
import MapKit

class ViewController: UIViewController {

    
    @IBOutlet weak var map: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
        let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(22.256412, 77.251032)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "My WorkPlace"
        annotation.subtitle = "Enjoy YOUR WORK"
        
        map.addAnnotation(annotation)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

MapKit : Show distance between two location



STEP - 1 : Add a button to Main.Storyboard



STEP - 2 : ViewController.swift

//
//  ViewController.swift
//  Map
//
//  Created by Admin on 04/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBAction func showMeWhere(_ sender: Any) {
        let latitude : CLLocationDegrees = 28.441830
        let longitude : CLLocationDegrees = 76.258830
        
        let regionDistance : CLLocationDistance = 1000;
        let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
        
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinate, regionDistance, regionDistance)
        let options = [MKLaunchOptionsMapCenterKey : NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey : NSValue(mkCoordinateSpan : regionSpan.span)]
        
        let placemark = MKPlacemark(coordinate: coordinate)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "My House"
        mapItem.openInMaps(launchOptions: options)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}




STEP - 3 : Run the app and press on 'Click Me Where' button