Thursday 27 April 2017

Maps


import UIKit
import CoreLocation
import MapKit

class MapViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

    var locationManager: CLLocationManager?
    var userLocation : MKUserLocation?
    @IBOutlet weak var map: MKMapView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        
        locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        locationManager?.requestWhenInUseAuthorization()
        locationManager?.startUpdatingLocation()
        
        map.showsUserLocation = true
        
        map.delegate = self
        locationManager?.delegate = self

        // Do any additional setup after loading the view.
    }

   


    @IBAction func mapview(_ sender: Any) {
         //map.mapType = MKMapType.hybrid   // satellite view 
        takeScreenshot(view: map) . // screen shot
    }
    
    override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
        if motion == UIEventSubtype.motionShake {
            addAnnotation()
        }
    }
    
    func takeScreenshot(view: UIView) -> UIImageView {
        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        
        return UIImageView(image: image)
    }

    
    func addAnnotation() {
        if let uLocation = userLocation {
            let annotation = PizzaAnnotation(coordinate: uLocation.coordinate, title: "Pizaa", subtitle: "Cheese")
            self.map.addAnnotation(annotation)
        }
    }
    
    
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600, 500)
        self.map.setRegion(region, animated: true)
        self.userLocation = userLocation
    }


}




Note: Must on Maps: Project -- > capabilities --> Maps





No comments:

Post a Comment

Remove bottom line in navigation bar

navigationController ?. navigationBar . setBackgroundImage ( UIImage (), for: . any , barMetrics: . default )          navigat...