getting the user’s locationcgi.csc.liv.ac.uk/~phil/teaching/comp228/practicals... · import...

3
COMP228/327 Week 8, w/c 11th November 2019 Getting the User’s Location Fo some projects it’s useful to be able to sort the items in a table based on their proximity to the user - such as the Artworks on Campus app we discussed in the lectures on Prototyping. But how do you determine the user’s location (and how, in particular, do we do it in the simulator)? Create a new single-view project using Storyboard (no need for CoreData or Unit tests) and place a Map on your storyboard. Make sure that you control drag from the Map to the View Controller yellow icon, and make it a delegate. You’ll also need to control drag from the map to your view controller source and make an outlet for the map (I’m going to call it “myMap”). We will add the required frameworks to our ViewController source - edit yours as per the image below. Apps can’t just know where the user is located (it’s a potential intrusion on the user’s privacy), and so we have to request that the user to allow iOS to tell us. Select the info.plist from the list of files on the left hand side of your project window. You will need to add two additional items to the plist. Hover over the “Information Property List” title and click the plus symbol. From the popup list select “Privacy - Location When in Use Usage Description” and repeat this with “Privacy - Location Usage Description”. Add a meaningful string describing why you need each of these. Now we need to update the ViewController to add protocols for the delegates for the Map and Core Location. Update your ViewController.swift file to show the following (you can find the text later in this document):

Upload: others

Post on 22-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting the User’s Locationcgi.csc.liv.ac.uk/~phil/Teaching/COMP228/practicals... · import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate,

COMP228/327 Week 8, w/c 11th November 2019

Getting the User’s Location Fo some projects it’s useful to be able to sort the items in a table based on their proximity to the user - such as the Artworks on Campus app we discussed in the lectures on Prototyping. But how do you determine the user’s location (and how, in particular, do we do it in the simulator)?

Create a new single-view project using Storyboard (no need for CoreData or Unit tests) and place a Map on your storyboard. Make sure that you control drag from the Map to the View Controller yellow icon, and make it a delegate.

You’ll also need to control drag from the map to your view controller source and make an outlet for the map (I’m going to call it “myMap”).

We will add the required frameworks to our ViewController source - edit yours as per the image below.

Apps can’t just know where the user is located (it’s a potential intrusion on the user’s privacy), and so we have to request that the user to allow iOS to tell us.

Select the info.plist from the list of files on the left hand side of your project window. You will need to add two additional items to the plist. Hover over the “Information Property List” title and click the plus symbol. From the popup list select “Privacy - Location When in Use Usage Description” and repeat this with “Privacy - Location Usage Description”. Add a meaningful string describing why you need each of these.

Now we need to update the ViewController to add protocols for the delegates for the Map and Core Location. Update your ViewController.swift file to show the following (you can find the text later in this document):

Page 2: Getting the User’s Locationcgi.csc.liv.ac.uk/~phil/Teaching/COMP228/practicals... · import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate,

So that the simulator has a local location to use (rather than default to California) we can import an Ashton Building location document (see the “Ashton Building Location” link on the COMP228/327 web page). Download it and add it to your project in the usual way.

We need to setup CoreLocation so that it will start sending us location messages when the user’s location changes. We have to write some code to create a locationManager instance, and then set it up with some reasonable default values. We then have to request the user let us get their location, and if they confirm it, we will get location messages passed to us (calling a method that we have to supply). Here’s my full code from ViewController.swift.

// // ViewController.swift // User Location // // Created by Phil Jimmieson on 14/11/2019. // Copyright © 2019 Phil Jimmieson. All rights reserved. //

import UIKit import MapKit import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var locationManager = CLLocationManager() //create an instance to manage our the user’s location.

@IBOutlet var myMap: MKMapView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locationManager.delegate = self as CLLocationManagerDelegate //we want messages about location locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation locationManager.requestWhenInUseAuthorization() //ask the user for permission to get their location locationManager.startUpdatingLocation() //and start receiving those messages (if we’re allowed to) } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let locationOfUser = locations[0] //get the first location (ignore any others) let latitude = locationOfUser.coordinate.latitude let longitude = locationOfUser.coordinate.longitude let latDelta: CLLocationDegrees = 0.002 let lonDelta: CLLocationDegrees = 0.002 let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta) let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let region = MKCoordinateRegion(center: location, span: span) self.myMap.setRegion(region, animated: true) }

}

Don’t forget to set the Map properties correctly (to display the user’s location. Also I like to use the hybrid map):

Finally, you should alt-click on the name of the simulator you’re about to run this on - this will open a panel where you can configure various run time options. Select the Ashton building as the default location. (The

Page 3: Getting the User’s Locationcgi.csc.liv.ac.uk/~phil/Teaching/COMP228/practicals... · import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate,

first time I tried this it did not work, and it was not until the App had run and stopped that I had the location option).

You should now be able to run this App in the simulator and you should be prompted to permit it to provide your (simulated) location to our app:

If you click allow, the map should zoom in to the Ashton building.

Please note, this lab doesn’t count towards your portfolio, but will be of use to you for assignment 2.