【问题标题】:How to add an UIButton to placemark? ios objective c如何将 UIButton 添加到地标? ios目标c
【发布时间】:2013-10-28 09:52:44
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c uibutton mkannotation mkannotationview


    【解决方案1】:

    在将 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...
    
    }
    

    希望对你有用

    【讨论】:

    • 感谢您的快速回复。简单地将这两种方法添加到我的代码中不会添加按钮。 viewDidLoad中需要做什么?
    • @user2920762:您需要在注释视图中添加按钮对吗?
    • 在互联网上搜索 MKAnnotationView 后,我明白了上面的代码是如何工作的。但是,我的图钉仍然显示在地图上,标题与 viewDidLoad 中的所有设置一样,但标题右侧仍然没有按钮。好像您的建议没有被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多