Thank you for visiting our account. We are going to make a map app in an hour. If would you like to study yourself before hands-on, or review what you have learned in the session, please use the following material.
We are providing free hands-on on a monthly basis
https://www.meetup.com/iOS-Development-Meetup-for-Beginner/
We also hold face-to-face or group lesson for individual interested in making iOS app themselves
http://ios-class-for-beginner.esy.es/
Xcode 8.3.2 / Swift 3
0-1. Open Xcode 0-2. Select Create a new Xcode project or Go to
File
→New
→Project...
0-3. Select Single View Application and then tap Next
0-4. Fill Product name and then tap Next
0-5. Select the place for saving your project and then tap Create
2-1. Add a
MKMapView
to your View Controller
2-2. Add a
UISegmentControl
to your View Controller
- Increase the number of segments to 3
- Rename each segment to Map, Hybrid, and Satellite respectively
2-3. Apply autoresizing for map and segment control
★ control + drag in storyboard to create a control segue
3-1. Connect "MKMapView"
3-2. Connect "UISegmentControl"
★ It's preferable to write following code yourself. It will help you to understand code more.
import UIKit
import MapKit // Import MapKit to use your MKMapViw
import CoreLocation // Import CoreLocation to access GPS
class ViewController: UIViewController {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
var isCentered = false
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization() // Required in order to access GPS coordinates
}
func centerMapOnLocation(coordinate: CLLocationCoordinate2D) {
let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
self.map.setRegion(region, animated: true)
}
@IBAction func mapTypeChanged(sender: UISegmentedControl) {
switch (sender.selectedSegmentIndex) {
case 0:
map.mapType = MKMapType.standard
case 1:
map.mapType = MKMapType.hybrid
case 2:
map.mapType = MKMapType.satellite
default: break
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if !isCentered {
self.centerMapOnLocation(coordinate: locations.first!.coordinate)
isCentered = true
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways, .authorizedWhenInUse:
locationManager.startUpdatingLocation()
map.showsUserLocation = true
default:
locationManager.stopUpdatingLocation()
map.showsUserLocation = false
}
}
}