Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created December 13, 2012 03:59
Show Gist options
  • Save ChrisRisner/4273903 to your computer and use it in GitHub Desktop.
Save ChrisRisner/4273903 to your computer and use it in GitHub Desktop.
iOS Day 20
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation>{
CLLocationCoordinate2D _coordinate;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
#import "MapAnnotation.h"
@implementation MapAnnotation
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
self = [super init];
if (self != nil) {
_coordinate = coordinate;
}
return self;
}
@end
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *AnnotationViewID = @"annotationViewID";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
return annotationView;
}
- (IBAction)tappedAdd:(id)sender {
CLLocationCoordinate2D mapPoint;
mapPoint.longitude = -122.132;
mapPoint.latitude = 47.624300;
MKPointAnnotation *anny = [[MKPointAnnotation alloc] init];
anny.coordinate = mapPoint;
anny.title = @"Annotation description";
anny.subtitle = @"Annotation subtitle";
[self.mapView addAnnotation:anny];
}
- (IBAction)tappedRemove:(id)sender {
for (id annotation in self.mapView.annotations)
if (![annotation isKindOfClass:[MKUserLocation class]]) {
[self.mapView removeAnnotation:annotation];
}
}
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "CoreLocationController.h"
@interface ViewController : UIViewController <CoreLocationControllerDelegate, MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
@property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) CoreLocationController *locationController;
- (IBAction)tappedAdd:(id)sender;
- (IBAction)tappedRemove:(id)sender;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.locationController = [[CoreLocationController alloc] init];
self.locationController.delegate = self;
[self.locationController.locationManager startUpdatingLocation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment