-1

我有这个代码,我需要稍微修改一下。我在下面的代码在我当前的位置创建了一个图钉(正如我可能添加的那样)。

唯一的问题是我只需要在单击按钮时运行代码,而不是每次移动时运行。

这是当前位置和引脚的代码。

- (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
{
}
4

1 回答 1

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];

// 在视图控制器中创建定义为 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];

}
于 2014-05-29T02:38:24.540 回答