iPhone Location Aware A by Example Beginner''''s Guide - Pdf 11

www.it-ebooks.info
iPhone Location Aware Apps
by Example
Beginner's Guide
Build ve complete locaon-enabled apps from scratch—from
idea to implementaon!
Zeeshan Chawdhary
BIRMINGHAM - MUMBAI

www.it-ebooks.info
iPhone Location Aware Apps by Example
Beginner's Guide
Copyright © 2012 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark informaon about all of the
companies and products menoned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this informaon.
First published: March 2012
Producon Reference: 1160312
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-224-3

Manu Joseph
Producon Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
www.it-ebooks.info
About the Author
Zeeshan Chawdhary has been a keen developer for the last six years, and has worked
in the locaon-based space for the past ve years. He is currently the Chief Technology
Ocer of Wcies Inc, a San Francisco-based Locaon Content Provider. He is currently
experimenng with PostGIS, PhoneGap, and iOS 5, and is currently trying his hand at
blogging again at .
I would like to thank all the lovely people at PacktPub, especially Mary
Nadar for having introduced me to the PacktPub family.

A special thanks to Leena, Susmita, Lubna, and Llewellyn for working with
me relessly on the book.

I would also thank Christopher D. Sloop from WeatherBug, Lauren Sperber
and Janine Iamunno from AOL Patch.com, Tim Breidigan, and Robert
Marndale from Evenul.com – for having allowed me to use their
respecve APIs in the book, you guys rock man!
www.it-ebooks.info
About the Reviewers
Sebasan Borggrewe, born and raised in Germany, is a computer science Master's
student at the University of Edinburgh/RWTH Aachen. Since he was 16, he has been
freelancing for several web and mobile agencies, and has founded his own agency.
Currently, he is the co-founder and CTO of Loyalli Ltd., a London-based startup, developing
mobile loyalty card soluons (
).

teams. He specializes in .NET, iOS, and Android mobile development for clients in the
Washington, DC metropolitan area.
He can be reached at

Shuxuan Nie is a SOA Consultant, specializing in SOA and Java technologies. She has more
than 10 years of experience in the IT industry that includes SOA technologies, such as BPEL,
ESB, SOAP, XML, and Enterprise Java technologies, Eclipse plugins, and other areas, such as
C++ cross-plaorm development.
Since 2010, Shuxuan has been working in Rubiconred, and has been helping customers
resolve integraon issues.
From 2007 to 2010, Shuxuan had been working in Oracle Global Customer Support team,
and focussed on helping customers solve their middleware/SOA integraon problems.
Before joining Orcale, Shuxuan had been working in IBM China Soware Development Lab
for four years as sta soware engineer, where she parcipated in several complex products
on IBM Lotus Workplace, Webshpere and Eclipse plaorm, and then joined the Australia
Bureau of Meteorology Research Center, which is responsible for implementaon of
Automated Thunderstorm Interacve Forecast System for Aviaon and Defence.
Shuxuan holds an MS in the Computer Science degree from Beijing University of Aeronaucs
and Astronaucs.
www.it-ebooks.info
Marn Selva heads the Gaming team at Hungama Digital Media Entertainment Pvt Ltd,
with over nine years of experience. His passion, experience, and experse have also helped
him develop a keen interest in Product Development for Online & Devices.
At Hungama, Marn is responsible for building a gaming portal called
www.thegamebox.
com
, and heads a Gaming Studio that comprises of iOS Developers, PHP Developers, Game
Designers, and Content Writers.
He can be reached at
and

www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: The Locaon-based World 7
Understanding Locaon-based Services 8
Time for acon – consuming Locaon-based Services with Google 9
Buzzwords in the Locaon-based Industry 11
Applicaon of LBS and common use cases 13
Military 13
Government 13
Commercial 13
How Apple uses LBS in the iPhone, iPad, and iPod devices 14
iOS locaon API 15
Time for acon – turning o Locaon Tracking in your iPhone 15
Behind LBS – GPS 17
User segment 17
Space segment 18
Control segment 18
Push and Pull methods of Locaon Services 18
Push Service 18
Pull Service 19
Life without GPS: Wi-Fi-based locaon detecon 19
Life without GPS: cell ID posioning and cell tower triangulaon 21
Time for acon – using the SkyHook Wireless Loki framework to
determine your locaon 22
Life without GPS: Google Maps API 23
Understanding Indoor and Outdoor Navigaon 26
Summary 27
www.it-ebooks.info
Table of Contents

Region monitoring 73
Geocoding and reverse Geocoding – CLGeocoder 74
Direcon using heading 75
Core Locaon Manager – CLLocaonManager 76
Time for acon – checking for locaon service availability 77
User authorizaon 80
Time for acon – using Core Locaon with user authorizaon 81
The CLLocaon object 84
Time for acon – receiving locaon updates in your applicaon 85
Time for acon – boundary monitoring with Locaon Manager 87
Extending Hello Locaon for nearby events 90
Important things to know before we begin 91
www.it-ebooks.info
Table of Contents
[ iii ]
Time for acon – extending Hello Locaon for nearby events 91
Time for acon – Last.fm API in your app 97
What just happened? 98
Extending Hello Locaon for local search 99
Important things to know before we begin 100
Time for acon – building a local search app with foursquare 100
Summary 103
Chapter 4: Using Maps in your iOS apps—MapKit 105
Overview of the MapKit framework 105
Understanding map geometry 106
Time for acon – using MapKit in your app 108
Time for acon – using map gestures – panning and zooming 115
Annotang Maps – an overview 117
Time for acon – adding annotaons to your maps 117
Time for acon – draggable annotaons 119

Chapter 6: Events App—PacktEvents 179
PacktEvents: Overview and architecture 180
Architecture of PacktEvents 180
Storing and Retrieving Events with SQLite 181
Time for acon – storing and retrieving events with SQLite 182
Plong events on a map 191
Time for acon – plong events on a map 192
Filtering Events display by Event Categories 197
Time for acon – ltering Events by categories 198
Using the Event Kit framework to add events to your iPhone calendar 205
Time for acon – adding events to your iPhone calendar 207
Using the Twier framework 210
Time for acon – adding Twier capabilies to your iPhone app 210
Bonus: using the Layar Player API in your app: Augmented Reality 213
Time for acon – adding Augmented Reality to your iPhone app 213
PacktEvents: building the app 219
Summary 220
Chapter 7: Advanced Topics 221
Using direcons with locaon 222
Direcon using heading 222
Geng your app ready for direcon 222
Understanding heading using magnetometer 222
Time for acon – using heading for direcon in your app 223
Direcon using course 226
Time for acon – using course for direcon in your app 226
Core Moon: Moon Manager 229
How to use Core Moon 230
Time for acon – using MoonManager: accelerometer 231
Core Moon conclusion 235
Background app execuon 235

Find locaon by names 286
Time for acon – consuming the Patch News API 286
Adding the Geo Fencing support 295
Time for acon – adding the Geo Fencing support 296
Building our app - PacktNews 299
A bit on StoryBoard 299
Time for acon – building PacktNews 300
Summary 313
Chapter 10: Social Governance—TweetGovern 315
Social governance – an overview 316
TweetGovern – behind the scenes 316
Stackmob 318
Our approach: Twier 319
Icons and images 322
SDKs and frameworks 322
Time for acon – creang the UI for TweetGovern 323
Time for acon – detecng the user locaon and showing nearby issues 326
Time for acon – creang and vong for an issue 335
Summary 347
Appendix: Pop-Quiz Answers 349
Index 353
www.it-ebooks.info
www.it-ebooks.info
Preface
iPhone Locaon Aware Apps Beginner's Guide is probably the rst book from any technical
publisher that teaches you to build real world applicaons (ve of them). That's a bold step
from PacktPub - by undertaking more lively praccal examples, rather than 400 pages of
text! The book lays emphasis on locaon services, due to the ever-increasing role of locaon
in our day-to-day lives and increased geo-referenced content being produced/consumed on
the Internet and Mobiles. Be it news, sports or gossip, consumers no longer want to read/

Chapter 5, Weather App—WeatherPackt, builds a complete Weather App using WeatherBug
API. It also provides a Sengs page in the app to customize the Weather display. As a bonus
to the readers, we also included the Nuance Speech SDK for reading out the weather!
Chapter 6, Events App—PacktEvents, builds an Events app that shows us nearby events,
concerts, and gigs by Arsts, by using the excellent Evenul.com API. This chapter also shows
how to use the Twier API in iOS 5, and gives us a taste of Augmented Reality with the Layar
Player SDK.
Chapter 7, Advanced Topics, teaches us using direcons with locaon background services
including background locaon. It also explores the Moon Manager in iOS SDK, along with
Push and Local nocaons.
Chapter 8, Local Search—PacktLocal, works with the foursquare API to build a local search
app, with geo-fencing support.
Chapter 9, Locaon Aware News—PacktNews, uses the AOL's Patch News API to build a
hyperlocal news app. It uses the new iOS 5 Storyboarding feature in this applicaon, with
support for oine content using SQLite.
Chapter 10, Social Governance—TweetGovern. Twier provides the backbone for this
chapter and the accompanying app. We use Twier and hashtags for building the business
logic for our social governance app tled
tweetgovern. We learn to use the twier
re-tweeng concept as well, building upon our business logic.
What you need for this book
To run the examples and apps provided in the book, you will need a Mac running on Intel
Architecture with Xcode 4.2 or higher and iOS 5 installed on your iPhone or iPad.
Some examples need an API key, which is duly menoned at the beginning of the
chapter/topic.
www.it-ebooks.info
Preface
[ 3 ]
Who this book is for
Novice to professional level iOS programmers, who want to master locaon awareness and

}
www.it-ebooks.info
Preface
[ 4 ]
Any command-line input or output is wrien as follows:
2011-09-04 16:40:09.421 Hello Location GeoNames[3896:f803] Location
Inserted Cupertino
2011-09-04 16:40:33.977 Hello Location GeoNames[3896:f803] Location
Inserted Soho
2011-09-04 16:40:42.230 Hello Location GeoNames[3896:f803] Location
Inserted Wadala
2011-09-04 16:40:48.889 Hello Location GeoNames[3896:f803] Location
Inserted Cupertino
2011-09-04 16:40:55.913 Hello Location GeoNames[3896:f803] Location
Inserted Financial District
2011-09-04 16:41:04.692 Hello Location GeoNames[3896:f803] Location
Inserted Sydney CBD
New terms and important words are shown in bold. Words that you see on the screen,
in menus or dialog boxes for example, appear in the text like this: "Enter Hotels in San
Francisco as the search key and hit Enter".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to
develop tles that you really get the most out of.
To send us general feedback, simply send an e-mail to
, and
menon the book tle through the subject of your message.
If there is a topic that you have experse in and you are interested in either wring or

address or website name immediately so that we can pursue a remedy.
Please contact us at
with a link to the suspected pirated material.
We appreciate your help in protecng our authors, and our ability to bring you
valuable content.
Questions
You can contact us at if you are having a problem with any
aspect of the book, and we will do our best to address it.
www.it-ebooks.info
www.it-ebooks.info
1
The Location-based World
Locaon-Based Services will be worth $10 Billion by 2016 – GigaOm
Locaon-Based Services (LBS) are a revoluonary, but sll fresh from the oven,
breed of services that has grown tremendously to carve itself as a new industry
in just a few years.
Locaon-Based Services is the next step in the evoluon for search, on the web
and mobile, adding the Locaon Context (where am I or things around me) for
search. To quote from Wikipedia on the denion of LBS:
A Locaon-Based Services (LBS) is an informaon or entertainment service,
accessible with mobile devices through the mobile network and ulizing the
ability to make use of the geographical posion (read Geocodes or Latude/
Longitude) of the mobile device.
You may have already used LBS when on Twier, Facebook, Foursquare,
Groupon, or visit hyperlocal web pages such as
Wcities.com, Yelp.com,
Qype.co.uk, and Eventful.com to nd the top venues in the city or events
happening in your city.
Want to know how the locaon is determined? Connue reading the chapter to
understand the dierent locaon detecon methods and which one is the right

own posioning system as well, but GPS remains the most popular and preferred choice for
device makers and applicaon developers worldwide.
Anyone can use GPS freely by using either a Personal Navigaon Device (Garmin, TomTom),
or an In-Car Navigaon System (Ford Sync), or by using a Smart Phone.
On the mobile front, LBS also use Google Maps and other cartographic API services
extensively (even in cases where the device does not support GPS). This is done using rich
map data and Geocoding services. Using Geocoding and smart algorithms, a user's posion
can be guessed or approximated. Mobile Operang Systems, such as Android, further the
cause of LBS by integrang locaons into the Core OS, where the locaon can be fetched,
used, and updated by all applicaons.
Apple iOS leads the pack with the best soware API support, coupled with excellent
hardware and posioning system integrated in the Apple Eco System. It also has
network-based Assisted GPS (AGPS) that uses the network's data connecon in the
case of weak GPS signals as well as Apple's own Wi-Fi locaon database. iOS developers
have a plethora of locaon tools and API to work with.
www.it-ebooks.info
Chapter 1
[ 9 ]
In short, LBS can be described as a combinaon of two components, Locaon Providers
and Locaon Consumers, with GPS, AGPS, iOS API, and Google Maps API as the locaon
providers and GPS receivers, mobile phones, and websites as the consumers of locaon data.
Time for action – consuming Location-based Services with
Google
To understand how LBS work behind the scenes, let's take an example of the most common
use of LBS, that is, how Google.com uses LBS for its search.
1. Fire up your Safari Browser and navigate to .
2. Enter Hotels in San Francisco as the search key and hit Enter.
3. You are presented with results from the Google Places database, as shown in the
following screenshot:
www.it-ebooks.info


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status