MKAnnotation help


#1

Hi,
I am modifying the WhereAmI project, and I am trying to modify the annotations so that a long press drops a pin on the map. Can anyone give me any pointers? Here is the code
WhereAmIViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereAmIViewController : UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate,
UITextFieldDelegate>
{
    CLLocationManager *locationManager;
    
    IBOutlet MKMapView *worldView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
    __weak IBOutlet UIToolbar *bookmarks;
}
@property (strong, nonatomic) IBOutlet UIToolbar *toolBar;
- (IBAction)mapType:(id)sender;

-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;

@end

WhereAmIViewController.m

#import "WhereAmIViewController.h"
#import "BNRMapPoint.h"

@interface WhereAmIViewController ()

@end

@implementation WhereAmIViewController

-(id)initWithNibName:(NSString *)nibNameOrNil
              bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        //Create location manager object
        locationManager = [[CLLocationManager alloc]init];
        
        //Ignore the warning
        [locationManager setDelegate:self];
        
        //And we want it to be as accurate as possible
        //regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    }
    
    return self;
}

-(void)viewDidLoad
{
    [worldView setShowsUserLocation:YES];
}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
    
    //How many seconds ago was this new location created
    NSTimeInterval t = [[newLocation timestamp]timeIntervalSinceNow];
    
    /* CLLocationManagers will return the last found location of the device first,
     you don't want that data in this case. If this location was made more than 3 minutes ago, ignore it. */
    if (t < -180) {
        //This is cached date, you don't want it, keep looking
        return;
    }
    
    [self foundLocation:newLocation];
}

-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

- (IBAction)mapType:(id)sender
{
    switch (((UISegmentedControl *)sender).selectedSegmentIndex)
    {
        case 0:
        {
            worldView.mapType = MKMapTypeStandard;
            break;
        }
        case 1:
        {
            worldView.mapType = MKMapTypeSatellite;
            break;
        }
        default:
        {
            worldView.mapType = MKMapTypeHybrid;
            break;
        }
    }
}

-(void)findLocation
{
    //tells the locationManager to start looking for the current location
    [locationManager startUpdatingLocation];
    
    //Start the activity monitor spinning
    [activityIndicator startAnimating];
    
    //Updates the textFiled so the user can't input text
    [locationTitleField setHidden:YES];
}

-(void)foundLocation:(CLLocation *)loc
{
    //Assign the argument coordinate values
    CLLocationCoordinate2D coord = [loc coordinate];
    
    //Create and instance of BNRMapPoint with the current data
    BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:coord
                                                       title:[locationTitleField text]];
    //Add it to the map view
    [worldView addAnnotation:mp];
    
    //Zoom into this region
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];
    
    //Reset the UI
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
}


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //coordinate is assigned location values
    CLLocationCoordinate2D loc = [userLocation coordinate];
    
    //MKCoordinateRegionMakeWithSDistance specifies a region with (The center point of the new coordinate region,
    //The amount of north-to-south distance (measured in meters) to use for the span, and
    //The amount of east-to-west distance (measured in meters) to use for the span)
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 150, 150);
    
    //The region has a center point, and span of coordinates to display
    //The span provides implicit zoom
    [worldView setRegion:region animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self findLocation];
    
    [textField resignFirstResponder];
    
    return YES;
}

-(void)dealloc
{
    //Tell the location manager to stop sending us messages
    [locationManager setDelegate:nil];
}


- (void)viewDidUnload {
    bookmarks = nil;
    [super viewDidUnload];
}
@end

BNRMapPoint.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject <MKAnnotation>
{

}
// A new designated initializer for instances of BNRMapPoint
-(id)initWithCoordinate:(CLLocationCoordinate2D)c
                  title:(NSString *)t;


//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotataion
@property (nonatomic, copy) NSString *title;

@property (nonatomic, readonly, copy) NSString *subtitle;

@end

BNRMapPoint.m

#import "BNRMapPoint.h"

@implementation BNRMapPoint

@synthesize coordinate, title, subtitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c
                  title:(NSString *)t
{
    self = [super init];
    
    if (self) {
        coordinate = c;
        [self setTitle:t];
        
        NSDate *theDate = [NSDate date];
        
        subtitle = [NSDateFormatter localizedStringFromDate:theDate
                                                  dateStyle:NSDateFormatterMediumStyle
                                                  timeStyle:NSDateFormatterMediumStyle];

    }
    
    return self;
}

@end