-1

I have this code that I need altered slightly. The code I have below creates a pin on my current location (perfectly as I might add).

The only problem is that I need the code only to be run when I click a button, not every time I move.

Here is the code of the current location and the pin.

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.mapView setDelegate:self];
    [self.mapView setShowsUserLocation:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    // zoom to region containing the user location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 700, 700);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    // add the annotation
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Your Car";
    //point.subtitle = @"";
    [self.mapView addAnnotation:point];
}

I need this to run when I click a button such as:

-(IBAction)addPin
{
}
Nicholas Gibson
  • 306
  • 1
  • 3
  • 11

1 Answers1

1

In the viewDidLoad: method,

Create a button like this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
       action:@selector(showPinOnClick)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 200.0, 150.0, 60.0);
[self.view addSubview:button];

// create the method defined as selector method for the UIButton in your viewcontroller

-(void) showPinOnClick{
//paste your code here to show the pin

CLLocationCoordinate2D location = self.mapview.userLocation.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;

location.latitude  = -32.008081;
location.longitude = 115.757671;

span.latitudeDelta = 0.03;
span.longitudeDelta = 0.03;

region.span = span;
region.center = location;


[_mapview setRegion:region animated:YES];
[_mapview regionThatFits:region];

//Create your annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = location;

point.title = @"Your Car";


//Drop pin on map
[_mapview addAnnotation:point];
[_mapview selectAnnotation:point animated:NO];

}
Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29
  • The problem is that when I place my code in the void... there is no declaration of userLocation so it has errors. – Nicholas Gibson May 29 '14 at 02:45
  • So, you need to find the userLocation by yourself inside of that method or simply pass from other methods. – Bikram Thapa May 29 '14 at 02:49
  • Ok, see in the updated answer , this will take you to the point of your defined latitude and longitude – Bikram Thapa May 29 '14 at 02:55
  • Okay thank you so much! One error. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController showPinOnClick:]: unrecognized selector sent to instance 0x15e6100e0' – Nicholas Gibson May 29 '14 at 03:01
  • just remove the semicolon in the calling line of selector. – Bikram Thapa May 29 '14 at 03:04
  • Okay thank you so much works perfectly! Except I need this code to work like I originally had it... placing the pin on the current location... How would I do that? Thank you so much! – Nicholas Gibson May 29 '14 at 03:10
  • there's a way to get the user's current location: self.mapView.userLocation.location.coordinate.latitude; and same for longitude but this doesn't gets updated until the full map is downloaded so returns 0 early. So, the choice left for you is to use that delegate to store the updated user's location in the property of your view controller and update the latitude and longitude by checking that property – Bikram Thapa May 29 '14 at 03:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54686/discussion-between-nicholas-gibson-and-bikram). – Nicholas Gibson May 29 '14 at 03:17