Sie sind auf Seite 1von 29

CIS 55 –

iOS Programming
Manish Goel
Lecture Objectives

• Simple Map
• Current Location
• Find Address
• Get Directions
• Find Places Nearby
Mapping Basics

• Simple UIViewController app with the following controls


• Address field to type address
• Route field to provide step-by-step directions
• A MapView to display the map
• Locate, Directions and Nearby buttons

• Need the following:


• Include the CoreLocation and MapKit Frameworks
• This has to be done both in the Project settings and in the code
• Set the simulator to ‘fake’ current location

• For mapping, following objects / APIs are needed:


• CLLocationManager to get current location
• CLGeocoder to convert addresses to Latitude / Longitude
• CLPlacemark to show pins on map
• MKDirections to work with directions from source to destination
• MKLocalSearch to work with data about points of interest
An error is displayed
for the MapView control
Including CoreLocation
and MapKit resolves it
Create other outlets and
actions
Need to add properties in the Info.plist to seek
permission to use location – new from iOS 8

info.plist is a set of properties that are applied


at runtime
Click on the + of the first row – Information
Property List – which is the main key to
start adding new properties.
Add the highlighted properties from the drop down list box that appears.
Adding any value for the property will display the value as a custom message.
1. ViewController needs to extend CLLocationManager and MKMapView delegates.
2. Declare variables for Location Manager, Geocoder and Placemark
3. Find the authorizationStatus from the system settings in viewDidLoad to determine
if location can be tracked and shown and set maps delegate to be the same app
On allowing however, the
blue-dot showing the
current location is missing

The first time the app


comes up, it asks for
permission due to the
info.plist property
To fix – explicitly
include CoreLocation
and MapKit frameworks
in project settings
Also, to ‘fake’ the current
location for the simulator,
set the Location option in
the Project Scheme
Now current position set
to San Francisco in the US
– if needed zoom the
map to closer resolution
To locate the address, call the
Geocoder to convert to a
Lat/Long and on successful
completion, call the Point API
to put placemarks and
identity bubble
All of this is standard
code to get route
To get directions:

Make a directionsRequest
Add the current location as source
Add the found location as destination
Get the directions
On successful completion of request –
Get the route
Overlay line on route
Zoom the map to include start and end
Get the route steps and show them to screen
To get nearby points of interest – Make a LocalSearchRequest, set its region and filter,
annotate the results with pins and bubbles