我有这个代码,我需要稍微修改一下。我下面的代码在我当前的位置创建了一个引脚(完全像我可能添加的那样)。
唯一的问题是我只需要在单击按钮时运行代码,而不是每次移动时都运行。
这是当前位置和引脚的代码。
- (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];
}
当我点击按钮时,我需要运行它,例如:
-(IBAction)addPin
{
}
答案 0 :(得分:1)
在viewDidLoad:方法中,
创建一个这样的按钮:
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];
//创建定义为viewcontroller中UIButton的选择器方法的方法
-(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];
}