Day 47: Stanford CS193p – Core Location and MapKit

Day 46: Stanford CS193p – Core Location and MapKit

Screen Shot 2015-06-26 at 9.37.53 PM

Lecture 14:

Core Location:
Framework for managing location and heading

Screen Shot 2015-06-27 at 12.04.56 AM

Basic Object:
CLLocation
Properties: coordinate, altitude, horizontal/verticalAccuracy, timestamp, speed, course

var coordinate: CLLocationCoordinate2D
struct CLLocationCoordinate2D {
CLLocationDegrees latitude //Double
CLLocationDegrees longitude //Double
}

var altitude: CLLocationDistance
negative value means “below sea level”

Went into how close to that latitude/longitude is actual location, and negative values. Negative values mean the coordinate or altitude is invalid.

var horizontalAccuracy: CLLocationAccuracy
var verticalAccuracy: CLLocationAccuracy

Other values include:
kCLLocationAccuracyBestForNevigation
kCLLocationAccuracyBest
kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracy HundredMeters
kCLLocationAccuracyKilometer
kCLLocationAccuracyThreeKilometers

“More accuracy requires more power”

Talked about Speed, Course, and Time Stamp, in detail. Also looked into CLLocationManager, which CAN be tested in xCode’s simulator, which is very cool.

Looked at how to use CLLocationManager.

There are different ways to monitor location:
Accuracy-based continual updates
Updates only when “significant” changes in location occur
Region-based updates
Heading monitoring

Asking CLLocationManager what your hardware can do. There are several ways to do this.

Must request authorization to get location info before CLLocationManager can get it.

func requestWhenInUseAuthorization()
func requestAlwaysAuthorization()

Must add Info.plist entry for this to work:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

Accuracy-based continuous location monitoring:
var desiredAccuracy: CLLocationAccuracy
var distanceFilter: CLLocationDistance

Starting and stopping normal position monitoring
func startUpdatingLocation()
func stopUpdatingLocation()

Now get notified via CLLocationManager’s delegate
func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])

Phone can still track location even if phone is off. Built into GPS chip, and allows your battery to stay alive. Fitness apps use this to their benefit.

Error reporting to the delegate:
important to do.
func locationManager(CLLocationManager, didFailWithError: NSError)

Background:
still get updates
apps can be hungry, so be careful

Significant location change monitoring in CLLocationManager:
i.e., vehicles.
func startMonitoringSignificantLocationChanges()
func stopMonitoringSignificantLocationChanges()

Make sure to turnOff updating!

Get notified by delegate when those changes happen.

Region-based location monitoring in CLLocationManager:
func startMonitoringForRegion(CLRegion)
func stopMonitoringForRegion(CLRegion)
let cr = CLCircularRegion(center: CLLocationCoordinate2D,
radius: CLLocationDistance,
identifier: String)

CLBeaconRegion is for detecting when you are near another device.

Now get notified via the CLLocationManager’s Delegate

Region-monitoring also works if your application is not running.

var monitoredRegions: NSSet
*limited number of regionals you can monitor, but it’s big.

Beacon regions can also detect range from a beacon.
func startRangingBeaconsInRegion(CLBeaconRegion)

To be a beacon is a bit more involved.

MapKit
Screen Shot 2015-06-27 at 12.47.16 AM

Annotations can have a callout:
a white box that pops out when user presses on pin.

But callout can also have accessory views: a pic and i for info, but only when pressed.

There is flexibility here.

Talked MKMapView
Bringing in mapkit through code or storyboard, usually storyboard drag and drop.

MKAnnotation:
its property is readonly

Generally a good idea to add all your annotations up-front

What do annotations look like on map?
Screen Shot 2015-06-27 at 12.52.23 AM
Can change it so it doesn’t look like a pin

MKAnnotationView:
Screen Shot 2015-06-27 at 12.56.32 AM

Looked at some interesting Properties for MKAnnotationView

MKMapView:
configuring the map view’s display type
showing the user’s current location
restricting the user’s interaction with the map

Setting where the user is seeing the map from (in 3D)
var camera: MKMapCamera

Controlling the region the map is displaying
cal also set the center point only or set to show annotations

Searching for places in the world
can search by “natural language” strings asynchronously

MKMapItem:
can open a MKMapItem that you get back from a MKLocationSearch in the Maps app

Overlays

What are Overlays
MKOverlay protocol
Overlays are associated with MKOverlayRenderers via delegate

Built-in Overlays and Renderers for numerous shapes

Then Demo Time: Trax Demo
Returning fixed tracks and Way Points

Screen Shot 2015-06-27 at 1.15.19 AM

Advertisements

Published by: Travis Deaton

I studied International Relations at American University, volunteered for various organizations, and now planning to embark on an adventure of a lifetime. More on that is forthcoming. I paint on the side, you can find my work on fineartdeaton.wordpress.com.

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s