Skip to content

Instantly share code, notes, and snippets.

@aflores
Created November 7, 2015 03:43
Show Gist options
  • Save aflores/8964886a3ac0631dae6b to your computer and use it in GitHub Desktop.
Save aflores/8964886a3ac0631dae6b to your computer and use it in GitHub Desktop.
Map Demo iOS9 Udemy
//
// ViewController.swift
// Map Demo
//
// Created by Armando Flores on 11/6/15.
// Copyright © 2015 Armando Flores. All rights reserved.
//
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setInitalMap()
detectLongPressWithAction("addPin:")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setInitalMap() {
let lat:CLLocationDegrees = 25.71
let lng:CLLocationDegrees = -80.41
let loc:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat,lng)
let latDeltaDeg:CLLocationDegrees = 0.7
let lngDeltaDeg:CLLocationDegrees = 0.7
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDeltaDeg, lngDeltaDeg)
let region:MKCoordinateRegion = MKCoordinateRegionMake(loc,span)
map.setRegion(region, animated: true)
}
func detectLongPressWithAction(execAction:String) {
let uilpgr = UILongPressGestureRecognizer(target: self, action: Selector(execAction))
uilpgr.minimumPressDuration = 0.7 //press for 2 seconds
self.map.addGestureRecognizer(uilpgr)
}
func addPin(gRec: UILongPressGestureRecognizer) {
let touchPoint = gRec.locationInView(self.map)
let newCoord: CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoord
annotation.title = "Here"
annotation.subtitle = "lat: \(newCoord.latitude) long: \(newCoord.longitude)"
map.addAnnotation(annotation)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment