title: Creating map to show user location and annotations programmatically in Swift 3
body: <p>I'm trying to replicate what I had in another project where I used storyboards using only code. The view controller I'm struggling with is the map view controller.</p>
  
  <p>I'm trying to create a map which shows the user location, zooms to a suitable level around that location on launching and then allow the user to drop a pin on the map which will have a circle overlay around the location of the pin.</p>
  
  <p>I have managed to create the map and show user location, but am struggling setting the region to zoom on the location when the map launches. I have been struggling with this for a couple of days.</p>
  
  <p>I know I need to use CLlocation2D somewhere, but am not sure where/how to use this at the moment. </p>
  
  <p>I also tried to setup my map using functions, code so far is below (it's a botch job from different sources trying to make it fit my goal, so apologies if it's messy)</p>
  
  <pre><code>&gt; import UIKit 
  &gt; import MapKit 
  &gt; import CoreLocation
  &gt;
  &gt; class MapViewController: UIViewController, MKMapViewDelegate,
  &gt; CLLocationManagerDelegate {
  
  &gt;     var mapView: MKMapView!
  &gt;     var locationManager = CLLocationManager()
  &gt;     
  &gt; override func viewDidLoad() {
  &gt;         super.viewDidLoad()
  &gt;  
  &gt;         setupMapView()
  &gt; 
  &gt;     }
  &gt;     
  &gt;     override func viewWillAppear(_ animated: Bool) {
  &gt;         super.viewWillAppear(animated)
  &gt;         
  &gt;     }
  &gt; 
  &gt;     func setupMapView() {
  &gt;         let mapView = MKMapView()
  &gt; 
  &gt;         let leftMargin:CGFloat = 0
  &gt;         let topMargin:CGFloat = 0
  &gt;         let mapWidth:CGFloat = view.frame.size.width
  &gt;         let mapHeight:CGFloat = view.frame.size.height
  &gt; 
  &gt;         mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
  &gt; 
  &gt;         mapView.mapType = MKMapType.standard
  &gt;         mapView.isZoomEnabled = true
  &gt;         mapView.isScrollEnabled = true
  &gt;         mapView.showsCompass = true
  &gt;         mapView.showsScale = true
  &gt;         mapView.showsUserLocation = true
  &gt;         
  &gt;         let noLocation = CLLocationCoordinate2D()
  &gt;         let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
  &gt;         let viewLocation = MKCoordinateRegionMake(noLocation, span)
  &gt;         
  &gt;         
  &gt;         mapView.setRegion(viewLocation, animated: true)
  &gt;         print(viewLocation)
  &gt; 
  &gt;         view.addSubview(mapView)
  &gt;     }
  &gt;  
  &gt;     //add function to create circle overlay
  &gt; 
  &gt; //    override func didReceiveMemoryWarning() { //       
  &gt; super.didReceiveMemoryWarning() //        // Dispose of any resources
  &gt; that can be recreated. //    }
  &gt;     
  &gt; 
  &gt;     /*
  &gt;     // MARK: - Navigation
  &gt; 
  &gt;     // In a storyboard-based application, you will often want to do a little preparation before navigation
  &gt;     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  &gt;         // Get the new view controller using segue.destinationViewController.
  &gt;         // Pass the selected object to the new view controller.
  &gt;     }
  &gt;     */
  &gt; 
  &gt; }
  </code></pre>
  
  <p>For the pin, in storyboard I used </p>
  
  <blockquote>
    <p>@IBAction func pinDrop(_ sender: UILongPressGestureRecognizer)</p>
  </blockquote>
  
  <p>but am not sure how to translate this to programatic code.</p>
  
  <p>Any help would be great!</p>