OGeek|极客世界-中国程序员成长平台

标题: ios - 尝试更改 MKPointAnnotation 颜色但丢失标题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:27
标题: ios - 尝试更改 MKPointAnnotation 颜色但丢失标题

我正在尝试将我的图钉颜色更改为紫色,但当我这样做时,我会失去标题。代码是:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    //init the location manager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager requestWhenInUseAuthorization];
    self.mapView.showsUserLocation=YES;

    self.userGeoPoint=self.message[@"userLocation"];
    self.pinView = [[MKPointAnnotation alloc] init];

    self.pinView.title=self.message[@"fromUser"];
    self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
    self.pinView.coordinate=self.coord;
    //use a slight delay for more dramtic zooming
    [self performSelectorselector(addPin) withObject:nil afterDelay:0.5];
}

-(void)addPin{

    [self.mapView addAnnotation:self.pinView];
    [self.mapView selectAnnotation:self.pinView animated:YES];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}

-(MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid<MKAnnotation>)annotationPoint
{
    if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
        return nil;

    static NSString *annotationIdentifier = @"annotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
    pinView.pinColor = MKPinAnnotationColorPurple;
    //now we can throw an image in there

    return pinView;
}

我尝试为 MKPinAnnotation 设置标题属性,但没有。无论如何我可以解决这个问题吗?



Best Answer-推荐答案


viewForAnnotation中,需要将canShowCallout设置为YES(默认为NO): p>

pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;



几个不相关的点:

  1. 看起来您有一个名为 pinView 的属性,类型为 MKPointAnnotation,用于在 viewDidLoad 中创建注释对象。然后在 viewForAnnotation 中,您有一个名为 pinView 的局部变量,类型为 MKPinAnnotationView。虽然这里没有命名冲突,但它会导致并暗示一些概念上的困惑。 MKPointAnnotation 是一个注解 model 类,而 MKPinAnnotationView 是一个注解 view 类——它们是完全不同的东西。例如,最好将注释属性命名为 pinuserAnnotation
  2. viewForAnnotation中的这条注释:

    //now we can throw an image in there
    

    似乎暗示您此时可以在 View 中设置自定义图像。不建议在 MKPinAnnotationView 中设置自定义图像,因为该类旨在以三种颜色之一显示默认图钉图像。要使用自定义图像,请创建一个普通的 MKAnnotationView

关于ios - 尝试更改 MKPointAnnotation 颜色但丢失标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315590/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4