SlideShare a Scribd company logo
1 of 25
iPhone Developer Advance Program
                           Location based Application




                                 by Eakapong Kattiya
Monday, March 26, 12
ความรู้พื้นฐานที่จําเป็น

                 - Objective-C for iPhone Programming

                 - Basic UI iPhone Programming

                 - Model View Controller

                 - XML Parser

                 - Property List / SQLite




Monday, March 26, 12
เนื้อหาในหลักสูตร Location Based Application
               Core Location
               - CLLocationManager
                       CLLocationManagerDelegate
                       CLLocation
                         - CLLocationCoordinate2D
                       CLLocationDistance


               MapKit
               - MKMapView
                       MKMapViewDelegate
                       MKCoordinateRegion
                       MKCoordinateSpan

               - MKReverseGeocoder
                       MKReverseGeocoderDelegate




Monday, March 26, 12
What is location Based ?




Monday, March 26, 12
Location Based Services




Monday, March 26, 12
Maps & Navigation : TomTom Thailand




Monday, March 26, 12
Maps & Navigation : PapaGO Pro




Monday, March 26, 12
Information Services : Thai Yellow Pages Live 2.0




Monday, March 26, 12
Tracking Services : UPS Mobile




Monday, March 26, 12
Location Based Game !




                         http://www.coderunnergame.com/




Monday, March 26, 12
Core Location
                 CLLocationManager และ CLLocation
                 เป็น Class ที่ใช้ในการหาตําแหน่งพิกัดของเครื่อง
                 iPhone , iPod Touch หรือ iPad ว่าอยู่ที่ใดในโลก
                 (Geolocation) ซึ่งจะทําให้เราสามารถใช้ตําแหน่งนี้ใน
                 การเชื่อมต่อกับฐานข้อมูล เพื่อหาข้อมูลสถานที่ใกล้เคียง
                 หรือ เพื่อนของเราได้

                 โดยการหาพิกัดจะมีอยู่ 3 วิธี
                 1. GPS เฉพาะเครื่อง iPhone 3G ขึ้นไป
                     โดยเลือกจับสัญญานดาวเทียม 3 ตัวมาคํานวนหา
                 ตําแหน่ง ความแม่นยํา < 100 เมตร.

                 2. WiFi ผ่าน SkyHook ใช้ MAC Address Router ไป
                 ค้นหาในฐานข้อมูล ความแม่นยํา 600 - 900 เมตร.

                 3. เสาสัญญาณมือถือ 4-5 สถานีใกล้เคียง ผ่านฐาน
                 ข้อมูล Google Map ความแม่นยํา 1 กิโลเมตร.




Monday, March 26, 12
CLLocation


             ใช้สําหรับจัดการเก็บข้อมูลพิกัด 2D Latitude , Longitude , หาค่าความเร็วในการเคลื่อนที่

             โดยวิธีการสร้าง Class ดังนี้

       [[CLLocation alloc] initWithLatitude:13.804992                   longitude:100.560758];




Monday, March 26, 12
CLLocation Property

            - coordinate
             ใช้สําหรับเก็บ พิกัด Latitude , Longitude ซึ่งจะเป็น Property ของ CLLocation อีกที

       CLLocationCoordinate2D coordinate
          coordinate.latitude = 13.804992 ;
          coordinate.longitude = 100.560758 ;




Monday, March 26, 12
CLLocation Property
            - speed
            - timestamp
             ใช้สําหรับคํานวนหาค่าความเร็วในการเคลื่อนที่ระหว่าง 2 พิกัด โดยเปรียบเทียบระยะห่าง ของ
             2 พิกัด และเวลา TimeStamp

       -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
       *)newLocation fromLocation:(CLLocation *)oldLocation
       {
          //simply get the speed provided by the phone from newLocation
           double gpsSpeed = newLocation.speed;

           // alternative manual method
           if(oldLocation != nil)
           {
           CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
               NSTimeInterval sinceLastUpdate = [newLocation.timestamp
       timeIntervalSinceDate:oldLocation.timestamp];
               double calculatedSpeed = distanceChange / sinceLastUpdate;

           }
       }




Monday, March 26, 12
CLLocationDistance


           ใช้สําหรับคํานวนหาระยะห่างระหว่าง 2 พิกัด มีหน่วยเป็นเมตร โดยเปรียบเทียบระยะห่าง 2D
           ของ 2 พิกัด


     CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];




Monday, March 26, 12
CLLocationManager
             วิธีใช้งาน
             1. Add Framework <Core Location>
             2. #import <CoreLocation/CoreLocation.h>
                 #import <CoreLocation/ CLLocationManagerDelegate.h>

             //สร้าง Object
             3. CLLocationManager *myLocationManager =
                   [[CLLocationManager alloc] init ] ;

             //กําหนด delegate
             4. [myLocationManager setDelegate:self];

             //กําหนด ระดับความแม่นยํา
             5. [myLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];

             //เริ่มหาพิกัด
             6. [myLocationManager startUpdatingLocation];

             //เมื่อพบพิกัดจะส่งข้อมูลมาที่ delegate
             7. didUpdateToLocation delegate


Monday, March 26, 12
CLLocationManagerDelegate

                didUpdateToLocation
                 ถูกเรียกใช้ตอนที่ CLLocationManager ตรวจพบว่ามีการเปลี่ยนแปลงพิกัด โดยเรา
                สามารถสร้าง Object CLLocation มาเก็บค่าพิกัดที่เปลี่ยนแปลงนี้ได้

                - (void)locationManager:(CLLocationManager *)manager
                didUpdateToLocation:(CLLocation *)newLocation fromLocation:
                (CLLocation *)oldLocation
                {
                  self.myLocation = newLocation ;

                       NSLog(@”My latitude = %f”,myLocation.coordinate.latitude);

                }




Monday, March 26, 12
Core Location
            Demo




Monday, March 26, 12
MapKit




Monday, March 26, 12
Basic of MapKit

               1. Init Map (IB / Storyboard)
               2. Set Region (พื้นที)
                                    ่
               3. Add Annotation Pin (ปักหมุด)
               4. Reverse Geo Coder
               5. Routing




Monday, March 26, 12
1. Init Map (IB / Storyboard)
         -(void)viewDidAppear:(BOOL)animated{
         ! [super viewDidAppear:YES];
         !
         !   //Show UserLocation
            [myMapView setDelegate:self];
            [myMapView setMapType:MKMapTypeStandard];
             myMapView.showsUserLocation = YES;
         }




Monday, March 26, 12
2. Set Region (พื้นที)
                              ่
         -(void)viewDidAppear:(BOOL)animated{
         ! [super viewDidAppear:YES];
         !
         ! //Show UserLocation
               [myMapView setDelegate:self];
               [myMapView setMapType:MKMapTypeStandard];
                myMapView.showsUserLocation = YES;

               //span with Kilometre
         !     MKCoordinateRegion *region ;
               region = MKCoordinateRegionMakeWithDistance
                             (myMapView.coordinate, 1000, 1000);
         !
         !     [myMapView setRegion:region animated:YES];
         }

         #pragma mark MKMapView Delegate
         - (void)mapView:(MKMapView *)mapView
         ! didUpdateUserLocation:(MKUserLocation *)userLocation
         {
         !
         ! [self setNewLocationCenter:userLocation.location];




Monday, March 26, 12
3. Add Annotation Pin (ปักหมุด)
         -(void)addPin{
             MyLocation *annotation = [[MyLocation alloc]
                    initWithName:crimeDescription
                         address:address
                      coordinate:coordinate] ;

               [myMapView addAnnotation:annotation];
         }

         - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
          {
             static NSString *identifier = @"MyLocation";
             if ([annotation isKindOfClass:[MyLocation class]]) {

                       MKPinAnnotationView *annotationView =
                         (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
                       if (annotationView == nil) {
                            annotationView =
                             [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                       } else {
                            annotationView.annotation = annotation;
                       }

                       annotationView.enabled = YES;
                       annotationView.canShowCallout = YES;
                       annotationView.image=[UIImage imageNamed:@"arrest.png"];
                        //here we use a nice image instead of the default pins

                       return annotationView;
               }

               return nil;
         }




Monday, March 26, 12
4. Reverse Geo Coder
      CLGeocoder แปลง พิกัดเป+นชื่อสถานที่
      -(void)reverseGeo{
          CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
          [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
          {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];

                  NSLog(@"Address   =%@",placemark.name) ;
                  NSLog(@"Address   =%@",placemark.administrativeArea) ;
                  NSLog(@"Address   =%@",placemark.locality) ;
                  NSLog(@"Address   =%@",placemark.postalCode) ;
                  NSLog(@"Address   =%@",placemark.subLocality) ;
                  NSLog(@"Address   =%@",placemark.subAdministrativeArea) ;
                  NSLog(@"Address   =%@",placemark.locality) ;
                  NSLog(@"Address   =%@",placemark.thoroughfare) ;
                  NSLog(@"Address   =%@",placemark.subThoroughfare) ;

                  NSString *currentAddress = [NSString stringWithFormat:@"%@,%@,%@",placemark.name,
                                    placemark.administrativeArea,
                                    placemark.locality,
                                    placemark.postalCode];

            }];
      }




Monday, March 26, 12
5. Routing
       -(void)showRoute{
        NSString* url =
           [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@@%f,%f&daddr=%@@%f,%f",
            [currentAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
            currentLocation.latitude,
            currentLocation.longitude,
            [place.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
            place.coordinate.latitude,
            place.coordinate.longitude];

             NSLog(@"Call openURL at %@",url);

       !    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
       }




Monday, March 26, 12

More Related Content

More from Eakapong Kattiya

Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Eakapong Kattiya
 
Android Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTAndroid Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTEakapong Kattiya
 
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework Eakapong Kattiya
 
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App StoreEakapong Kattiya
 
Iphone developer advance twitter
Iphone developer advance   twitterIphone developer advance   twitter
Iphone developer advance twitterEakapong Kattiya
 
iOS Advance Development - Social Media
iOS Advance Development - Social MediaiOS Advance Development - Social Media
iOS Advance Development - Social MediaEakapong Kattiya
 

More from Eakapong Kattiya (8)

Android basic 2 UI Design
Android basic 2 UI DesignAndroid basic 2 UI Design
Android basic 2 UI Design
 
Android basic 3 Dialogs
Android basic 3 DialogsAndroid basic 3 Dialogs
Android basic 3 Dialogs
 
Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )
 
Android Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTAndroid Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADT
 
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
 
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
 
Iphone developer advance twitter
Iphone developer advance   twitterIphone developer advance   twitter
Iphone developer advance twitter
 
iOS Advance Development - Social Media
iOS Advance Development - Social MediaiOS Advance Development - Social Media
iOS Advance Development - Social Media
 

Iphone developer advance location based

  • 1. iPhone Developer Advance Program Location based Application by Eakapong Kattiya Monday, March 26, 12
  • 2. ความรู้พื้นฐานที่จําเป็น - Objective-C for iPhone Programming - Basic UI iPhone Programming - Model View Controller - XML Parser - Property List / SQLite Monday, March 26, 12
  • 3. เนื้อหาในหลักสูตร Location Based Application Core Location - CLLocationManager CLLocationManagerDelegate CLLocation - CLLocationCoordinate2D CLLocationDistance MapKit - MKMapView MKMapViewDelegate MKCoordinateRegion MKCoordinateSpan - MKReverseGeocoder MKReverseGeocoderDelegate Monday, March 26, 12
  • 4. What is location Based ? Monday, March 26, 12
  • 6. Maps & Navigation : TomTom Thailand Monday, March 26, 12
  • 7. Maps & Navigation : PapaGO Pro Monday, March 26, 12
  • 8. Information Services : Thai Yellow Pages Live 2.0 Monday, March 26, 12
  • 9. Tracking Services : UPS Mobile Monday, March 26, 12
  • 10. Location Based Game ! http://www.coderunnergame.com/ Monday, March 26, 12
  • 11. Core Location CLLocationManager และ CLLocation เป็น Class ที่ใช้ในการหาตําแหน่งพิกัดของเครื่อง iPhone , iPod Touch หรือ iPad ว่าอยู่ที่ใดในโลก (Geolocation) ซึ่งจะทําให้เราสามารถใช้ตําแหน่งนี้ใน การเชื่อมต่อกับฐานข้อมูล เพื่อหาข้อมูลสถานที่ใกล้เคียง หรือ เพื่อนของเราได้ โดยการหาพิกัดจะมีอยู่ 3 วิธี 1. GPS เฉพาะเครื่อง iPhone 3G ขึ้นไป โดยเลือกจับสัญญานดาวเทียม 3 ตัวมาคํานวนหา ตําแหน่ง ความแม่นยํา < 100 เมตร. 2. WiFi ผ่าน SkyHook ใช้ MAC Address Router ไป ค้นหาในฐานข้อมูล ความแม่นยํา 600 - 900 เมตร. 3. เสาสัญญาณมือถือ 4-5 สถานีใกล้เคียง ผ่านฐาน ข้อมูล Google Map ความแม่นยํา 1 กิโลเมตร. Monday, March 26, 12
  • 12. CLLocation ใช้สําหรับจัดการเก็บข้อมูลพิกัด 2D Latitude , Longitude , หาค่าความเร็วในการเคลื่อนที่ โดยวิธีการสร้าง Class ดังนี้ [[CLLocation alloc] initWithLatitude:13.804992 longitude:100.560758]; Monday, March 26, 12
  • 13. CLLocation Property - coordinate ใช้สําหรับเก็บ พิกัด Latitude , Longitude ซึ่งจะเป็น Property ของ CLLocation อีกที CLLocationCoordinate2D coordinate coordinate.latitude = 13.804992 ; coordinate.longitude = 100.560758 ; Monday, March 26, 12
  • 14. CLLocation Property - speed - timestamp ใช้สําหรับคํานวนหาค่าความเร็วในการเคลื่อนที่ระหว่าง 2 พิกัด โดยเปรียบเทียบระยะห่าง ของ 2 พิกัด และเวลา TimeStamp -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {    //simply get the speed provided by the phone from newLocation     double gpsSpeed = newLocation.speed;     // alternative manual method     if(oldLocation != nil)     {     CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];         NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];         double calculatedSpeed = distanceChange / sinceLastUpdate;     } } Monday, March 26, 12
  • 15. CLLocationDistance ใช้สําหรับคํานวนหาระยะห่างระหว่าง 2 พิกัด มีหน่วยเป็นเมตร โดยเปรียบเทียบระยะห่าง 2D ของ 2 พิกัด CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation]; Monday, March 26, 12
  • 16. CLLocationManager วิธีใช้งาน 1. Add Framework <Core Location> 2. #import <CoreLocation/CoreLocation.h> #import <CoreLocation/ CLLocationManagerDelegate.h> //สร้าง Object 3. CLLocationManager *myLocationManager = [[CLLocationManager alloc] init ] ; //กําหนด delegate 4. [myLocationManager setDelegate:self]; //กําหนด ระดับความแม่นยํา 5. [myLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //เริ่มหาพิกัด 6. [myLocationManager startUpdatingLocation]; //เมื่อพบพิกัดจะส่งข้อมูลมาที่ delegate 7. didUpdateToLocation delegate Monday, March 26, 12
  • 17. CLLocationManagerDelegate didUpdateToLocation ถูกเรียกใช้ตอนที่ CLLocationManager ตรวจพบว่ามีการเปลี่ยนแปลงพิกัด โดยเรา สามารถสร้าง Object CLLocation มาเก็บค่าพิกัดที่เปลี่ยนแปลงนี้ได้ - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation { self.myLocation = newLocation ; NSLog(@”My latitude = %f”,myLocation.coordinate.latitude); } Monday, March 26, 12
  • 18. Core Location Demo Monday, March 26, 12
  • 20. Basic of MapKit 1. Init Map (IB / Storyboard) 2. Set Region (พื้นที) ่ 3. Add Annotation Pin (ปักหมุด) 4. Reverse Geo Coder 5. Routing Monday, March 26, 12
  • 21. 1. Init Map (IB / Storyboard) -(void)viewDidAppear:(BOOL)animated{ ! [super viewDidAppear:YES]; ! ! //Show UserLocation [myMapView setDelegate:self]; [myMapView setMapType:MKMapTypeStandard]; myMapView.showsUserLocation = YES; } Monday, March 26, 12
  • 22. 2. Set Region (พื้นที) ่ -(void)viewDidAppear:(BOOL)animated{ ! [super viewDidAppear:YES]; ! ! //Show UserLocation [myMapView setDelegate:self]; [myMapView setMapType:MKMapTypeStandard]; myMapView.showsUserLocation = YES; //span with Kilometre ! MKCoordinateRegion *region ; region = MKCoordinateRegionMakeWithDistance (myMapView.coordinate, 1000, 1000); ! ! [myMapView setRegion:region animated:YES]; } #pragma mark MKMapView Delegate - (void)mapView:(MKMapView *)mapView ! didUpdateUserLocation:(MKUserLocation *)userLocation { ! ! [self setNewLocationCenter:userLocation.location]; Monday, March 26, 12
  • 23. 3. Add Annotation Pin (ปักหมุด) -(void)addPin{ MyLocation *annotation = [[MyLocation alloc] initWithName:crimeDescription address:address coordinate:coordinate] ; [myMapView addAnnotation:annotation]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *identifier = @"MyLocation"; if ([annotation isKindOfClass:[MyLocation class]]) { MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; } else { annotationView.annotation = annotation; } annotationView.enabled = YES; annotationView.canShowCallout = YES; annotationView.image=[UIImage imageNamed:@"arrest.png"]; //here we use a nice image instead of the default pins return annotationView; } return nil; } Monday, March 26, 12
  • 24. 4. Reverse Geo Coder CLGeocoder แปลง พิกัดเป+นชื่อสถานที่ -(void)reverseGeo{ CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; NSLog(@"Address =%@",placemark.name) ; NSLog(@"Address =%@",placemark.administrativeArea) ; NSLog(@"Address =%@",placemark.locality) ; NSLog(@"Address =%@",placemark.postalCode) ; NSLog(@"Address =%@",placemark.subLocality) ; NSLog(@"Address =%@",placemark.subAdministrativeArea) ; NSLog(@"Address =%@",placemark.locality) ; NSLog(@"Address =%@",placemark.thoroughfare) ; NSLog(@"Address =%@",placemark.subThoroughfare) ; NSString *currentAddress = [NSString stringWithFormat:@"%@,%@,%@",placemark.name, placemark.administrativeArea, placemark.locality, placemark.postalCode]; }]; } Monday, March 26, 12
  • 25. 5. Routing -(void)showRoute{ NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@@%f,%f&daddr=%@@%f,%f", [currentAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], currentLocation.latitude, currentLocation.longitude, [place.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], place.coordinate.latitude, place.coordinate.longitude]; NSLog(@"Call openURL at %@",url); ! [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; } Monday, March 26, 12