This document summarizes a presentation on location services in iOS, including GPS, MapKit, Core Location, and region monitoring/geofencing. It provides code examples for initializing a location manager, activating GPS using Core Location, implementing region monitoring by defining regions and monitoring for boundary crossings, and important classes like CLCircularRegion for defining circular geo fences.
I os developers_meetup_4_sessionon_locationservices
1. Presenta(on
On
GPS,
Map-‐kit,
Core-‐Loca(on
and
Region
Monitoring,
Geo-‐fencing
2. Presented
By
Syed
Mahboob
Nur
iOS
Developer
Blog:
hEp://mahboobiosdeveloper.blogspot.com/
Fb
Profile:
hEps://www.facebook.com/mahboob.nur
3. GPS
The
Global
Posi(oning
System
(GPS)
is
a
satellite-‐based
naviga(on
system
made
up
of
a
network
of
24
satellites
placed
into
orbit
by
the
U.S.
Department
of
Defense.
GPS
was
originally
intended
for
military
applica(ons,
but
in
the
1980s,
the
government
made
the
system
available
for
civilian
use.
GPS
works
in
any
weather
condi(ons,
anywhere
in
the
world,
24
hours
a
day.
There
are
no
subscrip(on
fees
or
setup
charges
to
use
GPS.
4. Map
View
A
map
view
contains
a
flaEened
representa(on
of
a
spherical
object,
namely
the
Earth.
In
order
to
use
maps
effec(vely,
you
need
to
understand
a
liEle
bit
about
how
to
specify
points
in
a
map
view,
and
how
those
points
translate
to
points
on
the
Earth’s
surface.
Understanding
map
coordinate
systems
is
especially
important
if
you
plan
to
place
custom
content,
such
as
overlays
,
on
top
of
the
map.
6. Loca(on
Monitoring
Systems
• Core
Loca(on
• Region
Monitoring
• Geo
Fencing
• I
Beacon
And
so
on
7. Core
Loca(on
• The
Core
Loca(on
framework
lets
you
determine
the
current
loca(on
or
heading
associated
with
a
device.
The
framework
uses
the
available
hardware
to
determine
the
user’s
posi(on
and
heading.
You
use
the
classes
and
protocols
in
this
framework
to
configure
and
schedule
the
delivery
of
loca(on
and
heading
events.
You
can
also
use
it
to
define
geographic
regions
and
monitor
when
the
user
crosses
the
boundaries
of
those
regions.
In
iOS,
you
can
also
define
a
region
around
a
Bluetooth
beacon.
8. Core
Loca(on
Class
References
Protocol
References
Other
References
CLBeacon
CLBeaconRegion
CLCircularRegion
CLGeocoder
CLHeading
CLLoca9on
CLLoca9onManager
CLPlacemark
CLRegion
CLLLoca9onManagerDeleg
ate
Core
Loca9on
Func9ons
Core
Loca9on
Data
Types
Core
Loca9on
Constants
12. Problems
of
Ac(vate
GPS
all
the
(me
• Huge
BaEery
Consump(on
13. Loca(on
Monitoring
System
that
saves
BaEery
Life
• Region
Monitoring
• Geo
Fencing
14. Region
Monitoring
Region
monitoring
lets
you
monitor
boundary
crossings
for
defined
geographical
regions
and
Bluetooth
low
energy
beacon
regions.
(iOS
4.0
and
later.)
15. Steps
of
implemen(ng
Region
Monitoring
• Declare
and
Ini(alize
a
MKCoordinateRegion
• Declare
and
Ini(alize
a
CLCircularRegion
• Draw
a
circular
Geofence
around
the
current
loca(on
of
the
user.
• Start
monitoring
region
un(l
user
exit
the
region
• When
user
exit
the
region
draw
a
Circular
Geofence
and
start
monitoring
for
the
new
region.
16. Important
Codes
for
implemen(ng
Region
Monitoring
• MKCoordinateRegion
– A
structure
that
defines
which
por(on
of
the
map
to
display.
MKCoordinateRegion
region;
region.center.la(tude
=
loca(onManager.loca(on.coordinate.la(tude;
region.center.longitude
=
loca(onManager.loca(on.coordinate.la(tude;
region.span.la(tudeDelta
=
SPAN_VALUE;
region.span.longitudeDelta
=
SPAN_VALUE;
[self.mapView
setRegion:region
animated:YES];
17. Important
Codes
for
implemen(ng
Region
Monitoring
CLCircularRegion
:
The
CLCircularRegion
class
defines
the
loca(on
and
boundaries
for
a
circular
geographic
region.
You
can
use
instances
of
this
class
to
define
geo
fences
for
a
specific
loca(on.
The
crossing
of
a
geo
fence’s
boundary
causes
the
loca(on
manager
to
no(fy
its
delegate
CLCircularRegion
*myRegion=[[CLCircularRegion
alloc]initWithCenter:region.center
radius:10
iden(fier:@"myRegion"];
[loca(onManager
startMonitoringForRegion:myRegion];
18. Important
Codes
for
implemen(ng
Region
Monitoring
• Draw
a
circular
geofence:
MKCircle
*circle
=
[MKCircle
circleWithCenterCoordinate:region.center
radius:100];
[self.mapView
addOverlay:circle];
19. Important
Codes
for
implemen(ng
Region
Monitoring
• Delegate
method
to
make
Circular
Overlay
Colorful
-‐(MKOverlayView
*)
mapView:(MKMapView
*)mapView
viewForOverlay:(id<MKOverlay>)overlay
{
MKCircleView
*circleView
=
[[MKCircleView
alloc]
initWithCircle:overlay];
circleView.fillColor
=
[[UIColor
redColor]
colorWithAlphaComponent:0.1];
circleView.strokeColor
=
[[UIColor
blueColor]
colorWithAlphaComponent:1.0];
circleView.lineWidth
=
1;
return
circleView;
}
20. •
Important
Codes
for
implemen(ng
Region
Monitoring
Determine
when
user
enters
and
exits
region
-‐
(void)loca9onManager:(CLLoca9onManager
*)manager
didEnterRegion:(CLRegion
*)region
{
UIAlertView
*alert
=
[[UIAlertView
alloc]initWithTitle:@""
message:@"didEnterRegion"
delegate:nil
cancelBubonTitle:@"OK"
otherBubonTitles:nil,
nil];
[alert
show];
NSLog(@"didEnterRegion");
}
-‐
(void)loca9onManager:(CLLoca9onManager
*)manager
didExitRegion:(CLRegion
*)region
{
UIAlertView
*alert
=
[[UIAlertView
alloc]initWithTitle:@""
message:@"didExitRegion"
delegate:nil
cancelBubonTitle:@"OK"
otherBubonTitles:nil,
nil];
[alert
show];
}
21. Geofencing
• A
geo-‐fence
is
a
virtual
perimeter
for
a
real-‐
world
geographic
areas.
• A
geo-‐fence
could
be
dynamically
generated—
as
in
a
radius
around
a
store
or
point
loca(on.
Or
a
geo-‐fence
can
be
a
predefined
set
of
boundaries,
like
school
aEendance
zones
or
neighborhood
boundaries.
Custom-‐digi(zed
geofences
are
also
in
use.
23. iBeacon
• iBeacon
is
an
indoor
posi(oning
system
that
Apple
Inc
calls
"a
new
class
of
low-‐powered,
low-‐cost
transmiEers
that
can
no(fy
nearby
iOS
7
devices
of
their
presence.
• They
can
also
be
used
by
the
Android
opera(ng
system.[
The
technology
enables
an
iOS
device
or
other
hardware
to
send
push
no(fica(ons
to
iOS
devices
in
close
proximity.