Keeping Trackof Moving ThingsMapKit and CoreLocation in Depth
Today’s AgendaWorking with MapKit in iOSWorking with CoreLocation in iOSBattery Performance with GPSSome Basic GPS Tools f...
Getting Started Adding MapKit Framework to a Target Designing a View with MapView How to Center the MapView to a Location
SelectProjectTarget          Click Add +
Add a MapView to a View
Drop MapView on ViewController
Establish a Referencing Outlet
Create aReferencing  Outlet
Center the Map at a Location
CLLocationCoordinate2D location = {40.30444, -82.69556};[myMapView setRegion:MKCoordinateRegionMakeWithDistance(    locati...
Adding Annotations to a MapView Annotating a Map Custom Pins and Annotations Supporting Drag and Drop
Create a MKAnnotation
#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface GGTestMapAnnotation : NSObject <MKAnnotation> {    CL...
Add an Annotation to the Map
[myMapView addAnnotation:[[GGTestMapAnnotation alloc]  initWithCoordinate:location]];                                     ...
Become a MKMapViewDelegate
Create aDelegate Outlet
@interface GGFirstViewController : UIViewController  <MKMapViewDelegate>    [myMapView setDelegate:self];- (MKAnnotationVi...
Customize the Pin
[customAnnotationView setPinColor:MKPinAnnotationColorPurple];[customAnnotationView setAnimatesDrop:YES];                 ...
Create a Custom Pin Image
MKAnnotationView *customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] aut...
Supporting Drag and Drop
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{  ...
Do Something when Dropped
- (void)mapView:(MKMapView *)mapView    annotationView:(MKAnnotationView *)annotationView    didChangeDragState:(MKAnnotat...
Customizing Annotation Callouts Add and Auto Display Callout Add an Image and a Button to the Callout Do Something when Ta...
Add a Callout
- (NSString*) title;- (NSString*) subtitle;- (NSString*) title{   return @"CocoaConf 2012";}- (NSString*) subtitle{   retu...
Auto Display Callout
- (void)mapView:(MKMapView *)mapView     didAddAnnotationViews:(NSArray *)views{    [myMapView selectAnnotation:      [[my...
Add an Image to the Callout
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:    [UIImage imageNamed:@"m3-conference-left-callout.png"]]...
Add a Button to the Callout
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];[customAnnotationView setRightCalloutAccess...
Do Something when Tapped
- (void)mapView:(MKMapView *) mapView    annotationView:(MKAnnotationView *)view      calloutAccessoryControlTapped:(UICon...
Working with CoreLocationWorking with Multiple PointsDrawing a Line Between PointsDetermine the Distance and Direction
SelectProjectTarget          Click Add +
Core Location Data Types  typedef struct { ! CLLocationDegrees latitude; ! CLLocationDegrees longitude;  } CLLocationCoord...
Working with Multiple Points
double pointOneLatitude = 39.13333;double pointOneLongitude = -84.50000;CLLocationCoordinate2D pointOneCoordinate =   {poi...
Drawing a Line Between Points
MKMapPoint pointOne =   MKMapPointForCoordinate(pointOneCoordinate);MKMapPoint pointTwo =   MKMapPointForCoordinate(pointT...
- (MKOverlayView *) mapView:(MKMapView *) mapView       viewForOverlay:(id) overlay {    if ([overlay isKindOfClass:[MKPol...
Determine the Distance
CLLocation *pointOneLocation = [[CLLocation alloc]     initWithLatitude: pointOneCoordinate.latitude     longitude: pointO...
Determine the Direction
double lat1 = pointOneCoordinate.latitude * M_PI / 180.0;double lon1 = pointOneCoordinate.longitude * M_PI / 180.0;double ...
Battery Performance with GPS
Some Basic Tools for MacTranslating GPS Data  HoudahGPS (based on GPSBabel)Working with GPS Tracks  myTracks or RouteBuddy...
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Upcoming SlideShare
Loading in...5
×

Keeping Track of Moving Things: MapKit and CoreLocation in Depth

9,502

Published on

Adding a Map to an App and recording a User’s location as they use the App has become a common must have feature in may of todays popular applications. This presentation will go over the APIs for accomplishing such tasks including map annotations, dragging and dropping custom pins as well as delve into some of the finer aspects of the required location based calculations one needs to consider to find the center of the map or the distance between two points. Additionally the presentation will go over techniques to update a MapView with a moving object as well as positioning the image for the object properly along its heading. This will be a straight forward hands on development presentation with plenty of code examples.

Published in: Technology, Business
2 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,502
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
2
Likes
8
Embeds 0
No embeds

No notes for slide

Keeping Track of Moving Things: MapKit and CoreLocation in Depth

  1. 1. Keeping Trackof Moving ThingsMapKit and CoreLocation in Depth
  2. 2. Today’s AgendaWorking with MapKit in iOSWorking with CoreLocation in iOSBattery Performance with GPSSome Basic GPS Tools for MacWhat to expect with iOS 6
  3. 3. Getting Started Adding MapKit Framework to a Target Designing a View with MapView How to Center the MapView to a Location
  4. 4. SelectProjectTarget Click Add +
  5. 5. Add a MapView to a View
  6. 6. Drop MapView on ViewController
  7. 7. Establish a Referencing Outlet
  8. 8. Create aReferencing Outlet
  9. 9. Center the Map at a Location
  10. 10. CLLocationCoordinate2D location = {40.30444, -82.69556};[myMapView setRegion:MKCoordinateRegionMakeWithDistance( location,300000, 300000) animated:YES]; 1
  11. 11. Adding Annotations to a MapView Annotating a Map Custom Pins and Annotations Supporting Drag and Drop
  12. 12. Create a MKAnnotation
  13. 13. #import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface GGTestMapAnnotation : NSObject <MKAnnotation> { CLLocationCoordinate2D _coordinate;}- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;@end#import "GGTestMapAnnotation.h"#import <MapKit/MapKit.h>@implementation GGTestMapAnnotation@synthesize coordinate=_coordinate;- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{ self = [super init]; if (self != nil) { _coordinate = coordinate; } return self;}@end
  14. 14. Add an Annotation to the Map
  15. 15. [myMapView addAnnotation:[[GGTestMapAnnotation alloc] initWithCoordinate:location]]; 2
  16. 16. Become a MKMapViewDelegate
  17. 17. Create aDelegate Outlet
  18. 18. @interface GGFirstViewController : UIViewController <MKMapViewDelegate> [myMapView setDelegate:self];- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation: (id) annotation { MKPinAnnotationView *customAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; return customAnnotationView;} 3
  19. 19. Customize the Pin
  20. 20. [customAnnotationView setPinColor:MKPinAnnotationColorPurple];[customAnnotationView setAnimatesDrop:YES]; 4
  21. 21. Create a Custom Pin Image
  22. 22. MKAnnotationView *customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];[customAnnotationView setImage:[UIImage imageNamed:@"blue-arrow.png"]]; 5
  23. 23. Supporting Drag and Drop
  24. 24. - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{ _coordinate = newCoordinate;}[customAnnotationView setDraggable:YES]; 6
  25. 25. Do Something when Dropped
  26. 26. - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{ if (newState == MKAnnotationViewDragStateEnding) { NSLog(@"Do something when annotation is dropped"); }} 7
  27. 27. Customizing Annotation Callouts Add and Auto Display Callout Add an Image and a Button to the Callout Do Something when Tapped
  28. 28. Add a Callout
  29. 29. - (NSString*) title;- (NSString*) subtitle;- (NSString*) title{ return @"CocoaConf 2012";}- (NSString*) subtitle{ return @"Columbus, OH";}[customAnnotationView setCanShowCallout:YES]; 8
  30. 30. Auto Display Callout
  31. 31. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{ [myMapView selectAnnotation: [[myMapView annotations] lastObject] animated:YES];} 9
  32. 32. Add an Image to the Callout
  33. 33. UIImageView *leftIconView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"m3-conference-left-callout.png"]];[customAnnotationView setLeftCalloutAccessoryView:leftIconView]; 10
  34. 34. Add a Button to the Callout
  35. 35. UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];[customAnnotationView setRightCalloutAccessoryView:rightButton]; 11
  36. 36. Do Something when Tapped
  37. 37. - (void)mapView:(MKMapView *) mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ NSLog(@"Do Something when tapped");} 12
  38. 38. Working with CoreLocationWorking with Multiple PointsDrawing a Line Between PointsDetermine the Distance and Direction
  39. 39. SelectProjectTarget Click Add +
  40. 40. Core Location Data Types typedef struct { ! CLLocationDegrees latitude; ! CLLocationDegrees longitude; } CLLocationCoordinate2D; CLLocationDegrees - is a - double CLLocationDirection - is a - double CLLocationDistance - is a - double CLLocationAccuracy - is a - double
  41. 41. Working with Multiple Points
  42. 42. double pointOneLatitude = 39.13333;double pointOneLongitude = -84.50000;CLLocationCoordinate2D pointOneCoordinate = {pointOneLatitude, pointOneLongitude};[myMapView addAnnotation:[[[GGTestMapAnnotation alloc] initWithCoordinate:pointOneCoordinate] autorelease]];double pointTwoLatitude = 40.30444;double pointTwoLongitude = -82.69556;CLLocationCoordinate2D pointTwoCoordinate = {pointTwoLatitude, pointTwoLongitude};[myMapView addAnnotation:[[[GGTestMapAnnotation alloc] initWithCoordinate: pointTwoCoordinate] autorelease]]; 13
  43. 43. Drawing a Line Between Points
  44. 44. MKMapPoint pointOne = MKMapPointForCoordinate(pointOneCoordinate);MKMapPoint pointTwo = MKMapPointForCoordinate(pointTwoCoordinate);MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);pointArray[0] = pointOne;pointArray[1] = pointTwo;MKPolyline *routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];[self.myMapView addOverlay:routeLine]; 14
  45. 45. - (MKOverlayView *) mapView:(MKMapView *) mapView viewForOverlay:(id) overlay { if ([overlay isKindOfClass:[MKPolyline class]]){ MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; [polylineView setFillColor:[UIColor blueColor]]; [polylineView setStrokeColor:[UIColor blueColor]]; [polylineView setLineWidth:3]; MKOverlayView *overlayView = polylineView; return overlayView; }else { return nil; }} 15
  46. 46. Determine the Distance
  47. 47. CLLocation *pointOneLocation = [[CLLocation alloc] initWithLatitude: pointOneCoordinate.latitude longitude: pointOneCoordinate.longitude];CLLocation *pointTwoLocation = [[CLLocation alloc] initWithLatitude: pointTwoCoordinate.latitude longitude: pointTwoCoordinate.longitude];CLLocationDistance meters = [pointOneLocation distanceFromLocation: pointTwoLocation]; 16
  48. 48. Determine the Direction
  49. 49. double lat1 = pointOneCoordinate.latitude * M_PI / 180.0;double lon1 = pointOneCoordinate.longitude * M_PI / 180.0;double lat2 = pointTwoCoordinate.latitude * M_PI / 180.0;double lon2 = pointTwoCoordinate.longitude * M_PI / 180.0;double dLon = lon2 - lon1;double y = sin(dLon) * cos(lat2);double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);double radiansBearing = atan2(y, x);CLLocationDirection directionBetweenPoints = radiansBearing * 180.0/M_PI; 17
  50. 50. Battery Performance with GPS
  51. 51. Some Basic Tools for MacTranslating GPS Data HoudahGPS (based on GPSBabel)Working with GPS Tracks myTracks or RouteBuddyTranslating GPS Coordinates http://maps2.nris.mt.gov/topofinder1/LatLong.aspThe Google Geocoding API http://code.google.com/apis/maps/documentation/geocoding/

×