Post on 15-Jul-2015
transcript
iPhone LocationHow apps access the iPhone location and
what they can do with it
Outline
1. How an App Accesses iPhone Location
2. Location Information Provided
3. Alternatives to Receiving Location Updates
4. Working with Location
5. How iPhone Determines its Location
1. How an App Accesses iPhone
Location
● An iPhone app never calculates the phone’s location directly
● The operating system running on the phone determines the phone’s
location and any app that wants to use this information requests it from the
phone
● The usual way for an app to get the phone’s location is for the app to
receive periodic updates from the phone with the phone’s current location
● All apps running on a phone receive the same location updates
Listening to location updates
● When an app wants to start using location information, the app requests
that the phone begin sending location updates, and the app specifies the
level of accuracy that it needs
● The options are 3 km, 1 km, 100 meters, 10 meters, “Best”, or “Best for
Navigation”
● Requesting an appropriate level of accuracy is all about minimizing battery
drain
Requesting Accuracy Levels
● At a low of accuracy (e.g. 3 km), the phone could determine its location
solely from cellular or wifi signals, which would avoid powering up the GPS
and conserve battery life
● At higher levels of accuracy (e.g. 10 m), the phone listens to all available
signals that it can use to determine its location -- which includes GPS and
iBeacons, in addition to wifi and cellular
● Apple does not say specifically say which signals are used at which levels
of accuracy
o It could depend on how good the signals are at the time the app is running
Battery Drain
Best Level of Accuracy
● An app can force the use of GPS by specifying “Best” -- this will cause the
phone to use GPS constantly to determine location as accurately as
possible
● Requesting “Best for Navigation” goes one step further and causes the
phone to use GPS at its highest level of accuracy, plus any additional
hardware that could help
o Apple doesn’t say what additional hardware is used, but this could mean using something
like the accelerometer to detect when the phone has moved just a few feet to trigger a
location update
o The accelerometer is a sensor that detects acceleration of the phone in any direction. It is
very sensitive and will pick up even the slightest movement of the phone
Location Updates
● When the phone receives a request for location updates from an app, it
powers up any necessary hardware and starts calculating its location
based on the various signals
● From then on, the phone updates the app with a new location every time
the phone moves, until the app tells the phone it doesn’t need location
information any more
o How far the phone needs to move depends on the level of accuracy requested
● When no apps are requesting location updates, the phone shuts down the
GPS
● The first time a newly-installed app asks the phone for location updates,
the phone will ask the user to confirm that the app can access the user’s
location
● If the user says no, the app will not be able to receive location updates
from the phone
● The app will also be cut off from location updates if the user later changes
his iPhone settings to revoke the app’s access to location data or turn off
Location Services entirely
User Permissions
2. Location Information Provided
More than Just Location
● When the phone sends a location update to an app, it provides some
related information along with the phone’s current location
● Depending on the status of the various signals being received by the
phone, information provided can include:
o Estimated accuracy of the location
o Elevation, with an estimated accuracy
o Speed
o Course (direction of travel)
o Heading (direction the phone is pointing; actually comes in a separate update)
● The phone reports the phone’s location as decimal latitude and longitude
coordinates
● The phone also reports an estimated horizontal accuracy of the location in
meters
o The first few locations reported to an app may not have accuracy associated with them
because the GPS is still locking on to satellites
o Once it starts being reported, accuracy typically starts off relatively inaccurate (30 meters
or more), and gets better once the phone has a GPS lock
Location Accuracy
Location Accuracy
● When the phone has a good GPS
signal, reported horizontal accuracy is
typically 5 or 10 meters
o In practice, accuracy is often better than
this -- the error is usually less than 5
meters
● When viewing a map that shows the
user’s location as a blue dot, the outer
disc shown in light blue is the phone’s
estimated accuracy of the current
location
● When the phone has a lock on 4 satellites, it can determine its elevation
o With 3 satellites, it can only determine location
● Elevation is reported in meters, along with an estimated accuracy
● Vertical accuracy tends to be in the 3-6 meter range
● Despite being nominally in the same sub-10 meter range as location
accuracy, elevation accuracy is effectively less accurate because elevation
change for most applications is much smaller than horizontal distance
traveled
Elevation
● For example, here is an example bike ride recorded with a mobile app:
● Note that the horizontal distance is measured in miles while the vertical
distance is measured in feet -- so a 5-meter error in vertical measurement
would be more significant than the same error in horizontal measurement
Elevation Accuracy
● Speed is reported by the phone in meters per second for each location
reported
o The first few locations provided by the phone may not have speed associated with them
● Speed is not simply the distance / time from one location to the next -- it is
determined by the phone itself using a process Apple does not reveal
o It represents the phone’s speed at the moment the location is provided to an app, not the
average speed since the last location update
o The phone probably calculates this speed by combining GPS location with some other
information such as accelerometer data or an analysis of the GPS signal itself
Speed
Course & Heading
● The phone reports course with every location update, which is simply the
direction from one reported location to the next
o This would be pretty easy for an app to calculate, but the phone provides it with location
updates so the app doesn’t have to do the extra work
● An app can also access the phone’s heading, which is the direction the
phone is pointing at that moment
o Heading is provided in a separate update because it is based on different hardware than
location (it uses the phone’s magnetometer, which detects the orientation of the phone
relative to the earth in 3 dimensions by sensing magnetic fields like a compass)
o Heading is not very accurate because it can be subject to interference from external
magnetic fields, and possibly even the device itself
3. Alternatives to Receiving
Location Updates
● As an alternative to receiving updates every time the phone moves, an app
can request that it receive a location update only when the phone’s
location has changed “significantly”
● This will avoid powering up the GPS and will rely on cell towers and wifi
signals only -- resulting in substantially reduced power consumption
● “Significant” is not defined
o In a city with a lot of wifi networks, it could detect a change over a relatively short distance,
such as 100 meters
o Outside a city when relying on cell towers only, it might not trigger an update unless the
phone has moved 1 km or more
Significant Location Changes Only
● The usual method where an app receives an update every time the phone
moves only works while the app is still running (in the foreground or
background)
● Significant location change notifications can continue even after an app
has stopped running -- the phone will restart the app and let it run long
enough to process the location update
Location Updates When Not
Running
● Another alternative is region monitoring, where an app defines one or more
regions by specifying their center and radius
● Then the phone notifies the app whenever it enters or exits a region
● This uses wifi and cell towers only, so accuracy is similar to “significant
changes only” and regions will only work if they are fairly large
Region Monitoring
● iBeacons are small wireless sensors that can transmit data to an iPhone
using Bluetooth Low Energy (BLE)
● iBeacons allow apps to receive special promotions, coupons,
recommendations, etc. from businesses in real time when users are
nearby
● An app can display the information received from the business, or it can
make use of the knowledge that the iBeacon is nearby for navigational
purposes as a form of region monitoring
iBeacons
4. Working with Location
● When tracking the user’s path with high level of accuracy (e.g. to see how
far the user walked), any inaccuracies in location measurement will almost
always cause the path to be longer than it should:
● This is a running activity where the path recorded by the phone is shown in
red -- the user ran straight down the sidewalk, but clearly the red line is
longer
Inaccuracy Makes Distances Long
● To counteract the extra distance problem and provide a smoother path,
iPhone will “snap” location updates to a known road network when the user
is traveling above about 15 mph (on the assumption that the user is driving
or biking during that time)
● Apps have no way of knowing when this is happening -- all they see are
location updates like normal, but when the locations are later projected on
a map, it is clear when this was happening
“Snapping” to Roads
● In this example of a bike ride recorded with iPhone, you can clearly see
smooth portions of the path where the user was moving faster than the
threshold speed and jagged ones where he was moving slower
Snapped Path
● GPS requires line-of-sight communication, so anything that blocks a direct
path from the phone to a satellite can interfere with the signal
o This includes trees, mountains, buildings, car roofs, etc
● When a direct GPS signal is blocked and the user is near a large solid
object like a building, the GPS signal can arrive at the phone indirectly
after bouncing off the building
● Because GPS works by measuring very precisely how long the signal
takes to get from the satellite to the phone, when the signal takes a longer
path like this, the calculated location will be off
GPS Signal Quality
● Here, the path recorded by the
phone is quite poor where the
user was running among the
buildings, but becomes much
better where the user was getting
a better signal by the water
GPS Signal Quality
● The path recorded by the phone
can be shorter than the actual
path traveled if the phone loses
signal
● In this example, the user was
running laps around the lake and
lost signal during one of them --
causing the path to cut straight
across to the point where the
phone regained signal
Losing GPS Makes Distance Short
● It is difficult to overcome these limitations on the phone while the app is
running
● However, if the app is recording a user’s path and saving it to a server
(e.g. the way a fitness tracking app would), the server can make some
corrections after upload, such as
o Snapping to roads or other known locations
o Smoothing out jagged parts of the path
o Replacing the inaccurate elevation reported by the phone with known elevation at that
location
Post-Processing
● iPhone makes displaying maps in apps really easy
● Many common functions are built-in and require very little from the app
developer, including:
o Displaying user location on the map
o Displaying annotations and overlays on the map that zoom and scale with the map
o Centering the map on the user’s location
● Geocoding (getting latitude/longitude coordinates from an address) and
reverse geocoding (getting an address from latitude/longitude coordinates)
is also built-in and quite easy to use
Map Displays
5. How iPhone Determines its
Location
● iPhone can use 4 different types of signals to determine its location
o Cellular
o Wifi
o GPS
o Bluetooth (from iBeacons)
● The phone’s location is determined by combining one or more of these
signals
o Which signals are used depends on availability and the requested accuracy
● All of this happens behind the scenes -- any app can access the location of
the phone, but the app does not know how it was calculated
Four Types of Signals
● The location of cell towers is known to
a high degree of accuracy. Based on
the signal strength from various
towers, the phone can estimate how
far it is from each of them and
calculate the phone’s location
● This is always available when the
phone has cell coverage, but is not
very accurate since the towers can be
far away and distance based on
signal strength is not very accurate
Cellular
● The phone uses the same process to determine location using wifi signals
as it does for cellular signals, with distance estimated to the wifi hotspots
based on signal strength
● The locations of wifi hotspots have been crowdsourced for this purpose
● Using wifi is more accurate than cell towers, but the user needs to have
wifi turned on
Wifi
● When the phone detects a Bluetooth Low Energy signal from an iBeacon,
in addition to knowing that an iBeacon is nearby, the phone can use the
relative signal strength from multiple iBeacons to determine its location
using the same technique that it uses for cellular and wifi signals
● iBeacon signals only travel about 50 feet, so this location would be fairly
accurate
● This is particularly useful indoors, where GPS reception is poor
iBeacons
GPS
● GPS is the most accurate of the 4 signals, but it is slow to get started
● The phone needs to “lock on” to 3-4 satellites within its line of sight before
it can use GPS to determine location
o This can take anywhere from 15 seconds to several minutes, depending on the phone’s
view of the sky
o 3 satellites can provide location; a fourth provides elevation
o The phone uses its location calculated from other sources (e.g. cellular and wifi) to
determine which satellites are within its line of sight at the current time, speeding up this
process (this is called Assisted GPS, or A-GPS)
● Before the phone has a GPS lock, location is less accurate