Keeping Track of Moving Things: MapKit and CoreLocation in Depth

  • 7,313 views
Uploaded 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 …

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.

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • The source code for this presentation is located at github.com/ggeoffre/KeepingTrack
    Are you sure you want to
    Your message goes here
  • 'What to expect with iOS 6' chapter seems to be missing...
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
7,313
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
0
Comments
2
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Keeping Trackof Moving ThingsMapKit and CoreLocation in Depth
  • 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. Getting Started Adding MapKit Framework to a Target Designing a View with MapView How to Center the MapView to a Location
  • 4. SelectProjectTarget Click Add +
  • 5. Add a MapView to a View
  • 6. Drop MapView on ViewController
  • 7. Establish a Referencing Outlet
  • 8. Create aReferencing Outlet
  • 9. Center the Map at a Location
  • 10. CLLocationCoordinate2D location = {40.30444, -82.69556};[myMapView setRegion:MKCoordinateRegionMakeWithDistance( location,300000, 300000) animated:YES]; 1
  • 11. Adding Annotations to a MapView Annotating a Map Custom Pins and Annotations Supporting Drag and Drop
  • 12. Create a MKAnnotation
  • 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. Add an Annotation to the Map
  • 15. [myMapView addAnnotation:[[GGTestMapAnnotation alloc] initWithCoordinate:location]]; 2
  • 16. Become a MKMapViewDelegate
  • 17. Create aDelegate Outlet
  • 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. Customize the Pin
  • 20. [customAnnotationView setPinColor:MKPinAnnotationColorPurple];[customAnnotationView setAnimatesDrop:YES]; 4
  • 21. Create a Custom Pin Image
  • 22. MKAnnotationView *customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];[customAnnotationView setImage:[UIImage imageNamed:@"blue-arrow.png"]]; 5
  • 23. Supporting Drag and Drop
  • 24. - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{ _coordinate = newCoordinate;}[customAnnotationView setDraggable:YES]; 6
  • 25. Do Something when Dropped
  • 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. Customizing Annotation Callouts Add and Auto Display Callout Add an Image and a Button to the Callout Do Something when Tapped
  • 28. Add a Callout
  • 29. - (NSString*) title;- (NSString*) subtitle;- (NSString*) title{ return @"CocoaConf 2012";}- (NSString*) subtitle{ return @"Columbus, OH";}[customAnnotationView setCanShowCallout:YES]; 8
  • 30. Auto Display Callout
  • 31. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{ [myMapView selectAnnotation: [[myMapView annotations] lastObject] animated:YES];} 9
  • 32. Add an Image to the Callout
  • 33. UIImageView *leftIconView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"m3-conference-left-callout.png"]];[customAnnotationView setLeftCalloutAccessoryView:leftIconView]; 10
  • 34. Add a Button to the Callout
  • 35. UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];[customAnnotationView setRightCalloutAccessoryView:rightButton]; 11
  • 36. Do Something when Tapped
  • 37. - (void)mapView:(MKMapView *) mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ NSLog(@"Do Something when tapped");} 12
  • 38. Working with CoreLocationWorking with Multiple PointsDrawing a Line Between PointsDetermine the Distance and Direction
  • 39. SelectProjectTarget Click Add +
  • 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. Working with Multiple Points
  • 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. Drawing a Line Between Points
  • 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. - (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. Determine the Distance
  • 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. Determine the Direction
  • 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. Battery Performance with GPS
  • 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/