iOS开发中最基本的位置功能实现示例

2020-01-14 16:25:30王冬梅

    /** 
     *  是不是有点像自定义UITableViewCell一样 
     */  
    static NSString *identifier = @"annotation";  
    //  复用标注视图(MKPinAnnotationView是大头针视图,继承自MKAnnotation)  
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];  
    if (annotationView == nil) {  
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];  
    }  
    //  判断是否为自定义的标注视图  
    if ([annotation isKindOfClass:[HMTAnnotation class]]) {  
          
        //  设置大头针圆圈颜色  
        annotationView.pinColor = MKPinAnnotationColorGreen;  
        //  点击头针红色圆圈是否显示上面设置好的标题视图  
        annotationView.canShowCallout = YES;  
        //  要自定义锚点图片,可考虑使用MKAnnotationView;MKPinAnnotationView只能是以大头针形式显示!!!!  
        annotationView.image = [UIImage imageNamed:@"customImage"];  
        //  添加标题视图右边视图(还有左边视图,具体可自行查看API)  
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
        [button addTarget:self action:@selector(didClickAnnotationViewRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];  
        annotationView.rightCalloutAccessoryView = button;  
        //  是否以动画形式显示标注(从天而降)  
        annotationView.animatesDrop = YES;  
        annotationView.annotation = annotation;  
          
        //  返回自定义的标注视图  
        return annotationView;