Skip to content

A sample map application that enables you to toggle between different types of map view and locates you on the map

Notifications You must be signed in to change notification settings

iosClassForBeginner/mapApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Code Together: Let's make iPhone app in an hour

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.

Meetup

We are providing free hands-on on a monthly basis

https://www.meetup.com/iOS-Development-Meetup-for-Beginner/

Do you need a tutor?

We also hold face-to-face or group lesson for individual interested in making iOS app themselves

http://ios-class-for-beginner.esy.es/

Development Environment

Xcode 8.3.2 / Swift 3

Full procedure

0, Create your project

0-1. Open Xcode 0-2. Select Create a new Xcode project or Go to FileNewProject...
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

1, Design your app

🗂 Main.storyboard

2-1. Add a MKMapView to your View Controller

View Gif
>

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
View Gif
>

2-3. Apply autoresizing for map and segment control

View Gif
>

2, Connect UI components to the ViewController

🗂 Main.storyboard → ViewController.swift

★ control + drag in storyboard to create a control segue

3-1. Connect "MKMapView"

View Gif

3-2. Connect "UISegmentControl"

View Gif

4, Add code blocks in ViewController.swift

🗂 ViewController.swift

★ 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
        }
    }
}

About

A sample map application that enables you to toggle between different types of map view and locates you on the map

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages