User Tools

Site Tools


ios-labs-s14:class-08

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ios-labs-s14:class-08 [2014/02/26 13:18]
mbarboi
ios-labs-s14:class-08 [2014/02/28 14:55] (current)
mbarboi
Line 209: Line 209:
 ---- ----
  
 +=====Drawing on the Map=====
 +Lets trace the trips retrieved in the previous section onto the map. Each trip will have a red trace line and two pins, one for the start coordinate and one for the end coordinate.
 +
 +====Create Annotation Object====
 +  -Create new class MapAnnotation,​ subclass of NSObject
 +  -Have it implement the protocol MKAnnotation by typing "<​MKAnnotation>"​ immediately after the superclass name in the header file
 +  -Import it into MapRoot
 +  -Add the following properties to the header file
 +
 +<​code>​
 +@property (nonatomic, copy) NSString * title;
 +@property (nonatomic, copy) NSString * subtitle;
 +@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
 +</​code>​
 +
 +When the user touches the trips button (the car icon) you should add the annotations. The method for adding annotations to a map is called //​addAnnotation//​. Create a new annotation object for each start and stop coordinate and add it to the map.
 +
 +Note that the property called coordinate on the annotation object is of type CLLocationCoordinate2D-- you cannot pass the GPSCoordinate object! Instead, convert the lat and long from the GPSCoordinate object to the following helper function:
 +
 +  CLLocationCoordinate2DMake(LAT,​ LONG);
 +  ​
 +Which returns an object of type CLLocation2D
 +
 +====Create Overlay====
 +  -Copy the following methods into MapRoot:
 +
 +<​code>​
 +- (MKOverlayRenderer *)mapView:​(MKMapView *)mapView rendererForOverlay:​(id<​MKOverlay>​)overlay {
 +    //check to make sure its a polyline (this method is called for anything else thats drawn on a map)
 +    if (![overlay isKindOfClass:​[MKPolygon class]]) {
 +        MKPolyline *route = overlay;
 +        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:​route];​
 +        renderer.strokeColor = [UIColor colorWithRed:​1 green:0 blue:0 alpha:1];
 +        renderer.lineWidth = 3.0;
 +        return renderer;
 +    } else {
 +        return nil;
 +    }
 +}
 +
 +- (void) drawRouteForCoordinates:​(NSOrderedSet *) points{
 +    //draw the route for the map based on the coordiantes
 +    MKMapPoint* pointArray = malloc(sizeof(CLLocationCoordinate2D) * [points count]);
 +    ​
 +    //extract the CLLocation2D objects, add them all to a point in the reference frame of the map
 +    //note the array is a C array
 +    for(int i = 0; i < [points count]; i++) {
 +        GPSCoordinate *coordinate = [points objectAtIndex:​i];​
 +        ​
 +        double lat = [coordinate.lat doubleValue];​
 +        double lon = [coordinate.lon doubleValue];​
 +        ​
 +        MKMapPoint point = MKMapPointForCoordinate(CLLocationCoordinate2DMake(lat,​ lon));
 +        pointArray[i] = point;
 +    }
 +    ​
 +    // create the polyline based on the array of points, add to map
 +    [map addOverlay:​[MKPolyline polylineWithPoints:​pointArray count:​[points count]]];
 +    free(pointArray);​
 +}
 +</​code>​
 +
 +The first method is an optional delegate method for MKMapViews that returns a **renderer** specifying how the given object should be drawn. In this case we're making a thin red line. The second method is a little tricky; since the points in the line drawn are formed from structs, you need to make a C array instead of an Objective-C array to store the points. Didn't want to deal with that now.
 +
 +Call //​drawRouteForCoordinates//​ for each trip. Pass it the array of GPSCoordinate objects from the Trip object.
 +
 +----
 =====Conclusion===== =====Conclusion=====
 CoreData will not be that useful everyday, but if you ever have to write an app that has to deal with a **large** dataset it will make your life a lot easier. If you nail down the basics and make some nice wrapper methods for the data objects, it can also make Model management far easier. CoreData will not be that useful everyday, but if you ever have to write an app that has to deal with a **large** dataset it will make your life a lot easier. If you nail down the basics and make some nice wrapper methods for the data objects, it can also make Model management far easier.
ios-labs-s14/class-08.1393442336.txt.gz ยท Last modified: 2014/02/26 13:18 by mbarboi