Skip to content

Instantly share code, notes, and snippets.

@tkc
Created August 3, 2016 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkc/f1b2037a2cbe07b5e01583d8f9dfaf7d to your computer and use it in GitHub Desktop.
Save tkc/f1b2037a2cbe07b5e01583d8f9dfaf7d to your computer and use it in GitHub Desktop.
import MapKit
import UIKit
import CoreLocation
class MapViewController: UIViewController,CLLocationManagerDelegate{
private var locationManager:CLLocationManager!
private var mapView: MKMapView = MKMapView()
private var lat:CLLocationDegrees = 0
private var long:CLLocationDegrees = 0
private var latitudeDelta:CLLocationDegrees = 0
private var longitudeDelta:CLLocationDegrees = 0
private var pins:[PinModel] = []
override func viewDidLoad() {
super.viewDidLoad()
mapView.frame = self.view.frame
mapView.mapType=MKMapType.Standard
locationManager = CLLocationManager()
locationManager.delegate = self
self.view.addSubview(mapView)
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var statusStr = "";
switch (status) {
case .NotDetermined:
statusStr = "NotDetermined"
manager.requestWhenInUseAuthorization()
case .Restricted:
statusStr = "Restricted"
case .Denied:
statusStr = "Denied"
case .AuthorizedAlways:
statusStr = "AuthorizedAlways"
case .AuthorizedWhenInUse:
statusStr = "AuthorizedWhenInUse"
}
print(" CLAuthorizationStatus: \(statusStr)")
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latitude=manager.location!.coordinate.latitude;
let longitude=manager.location!.coordinate.longitude;
self.updateMapPoint(latitude,long:longitude)
}
func updateMapPoint(lat: CLLocationDegrees,long: CLLocationDegrees) {
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
mapView.setCenterCoordinate(center, animated: true)
let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let myRegion: MKCoordinateRegion = MKCoordinateRegionMake(center, mySpan)
mapView.region = myRegion
mapView.setCenterCoordinate(center, animated: true)
let data = self.getPin(lat,long:long,text: "Here!!");
mapView.addAnnotation(data)
}
func locationManager(manager: CLLocationManager,didFailWithError error: NSError){
print("error")
}
func getPin(lat: CLLocationDegrees,long: CLLocationDegrees,text:String) -> MKPointAnnotation {
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
let myPin: MKPointAnnotation = MKPointAnnotation()
myPin.coordinate = center
myPin.title = text
return myPin
}
}
class PinModel {
internal var latPoint: CLLocationDegrees = 0
internal var longPint: CLLocationDegrees = 0
internal var timeText: String=""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment