如何在MKMapView上添加按钮?

时间:2013-03-29 03:58:35

标签: iphone objective-c uibutton mkmapview

我正在尝试在地图上有按钮的iPhone应用程序,我该怎么做?我已经实施了MKMapView并设置了longitude/latitude并将其集中在我想要的位置。

2 个答案:

答案 0 :(得分:6)

你没有详细说明你的问题,所以我想你想要,

Use MKUserTrackingBarButtonItem

 - (void)viewDidLoad
    {
        [super viewDidLoad];    
        MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
        self.navigationItem.rightBarButtonItem = buttonItem;
    }

AMKUserTrackingBarButtonItem object是一个专门的栏按钮项,允许用户切换用户跟踪模式。例如,当用户点击按钮时,地图视图在跟踪具有和不具有标题的用户之间切换。如果在其他位置设置,该按钮还会反映当前用户跟踪模式。此栏按钮项目与单个地图视图相关联。

另请阅读此问题How to put a button on a Map in Iphone

答案 1 :(得分:1)

试试吧......

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != myMapView.userLocation) 
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation.pinColor = MKPinAnnotationColorRed;
        pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        infoButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
        [infoButton setBackgroundImage:[UIImage imageNamed:@"arrow_go_map.png"] forState:UIControlStateNormal];
        pinAnnotation.rightCalloutAccessoryView = infoButton;
        [infoButton addTarget:self action:@selector(detailBtnClk:) forControlEvents:UIControlEventTouchUpInside];
    }    
    return pinAnnotation;
}

希望我帮助