Question

How do I add a button to my placemarks and then get it to push onto a view controller?

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

            }
        }];
    }
}

I've had a look at MKAnotationView but struggling to understand how to get this working with CLPlacemark.

Was it helpful?

Solution

Add this before you add mapview to 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...

}

I hope it is useful to you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top