0

如何将按钮添加到我的地标,然后将其推送到视图控制器上?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set "More" logo in navigation bar
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Navigation"]];

    appDelegate=[[UIApplication sharedApplication] delegate];

    // Set longatude and latitude to tyne and wear
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(54.995184, -1.566699);

    // Set span to cover area
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);

    // Set region
    MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
    [self.nearbyMapView setRegion: regionToDisplay];

    for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
    {

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"addressline"];
        NSString *plotTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"title"];


        [geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks && placemarks.count > 0)
            {
                CLPlacemark *topResult = [placemarks objectAtIndex:0];
                MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];

                // Set title
                MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
                pa.coordinate = placemark.location.coordinate;
                pa.title = plotTitle;

                // Add placemark to map
                [self.nearbyMapView addAnnotation:pa];

            }
        }];
    }
}

我已经看过MKAnotationView但很难理解如何使用CLPlacemark.

4

1 回答 1

4

在将 mapview 添加到 self.view 之前添加它

-(void)viewDidLoad
{
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
    annotation.coordinate = yourCoordinate;
    annotation.title = @"Title";
    annotation.subtitle = @"SubTitle";
    [mapView1 addAnnotation:annotation];
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pinView = nil; 

        static NSString *defaultPinID = @"identifier";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

            annotationView.enabled = YES;
            pinView.canShowCallout = YES;

            UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            //Accessoryview for the annotation view in ios.
            pinView.rightCalloutAccessoryView = btn;
        }
        else
        {
            pinView.annotation = annotation;
        }
       pinView.annotation = annotation;
    return pinView;
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

    //Put your button stuff here...

}

我希望它对你有用

于 2013-10-28T09:58:59.640 回答