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.
    }


}


No comments:

Post a Comment