• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 无法在 MKPinAnnotationView 上设置标题

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:10:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已按照本教程进行操作: http://www.shawngrimes.me/2011/04/custom-map-pins-for-mapkit/#comment-193

但我无法添加标题和描述

(在此处查看我的代码 http://pastebin.com/03mDLc9q)



Best Answer-推荐答案


您正在尝试将名称和描述设置为注释 View 的属性,您应该在注释对象上使用 titlesubtitle - MyAnnotationClass,注解 View 在渲染标注时将使用该对象的标题和副标题。

我更改了您的代码以在此处工作:http://pastebin.com/YRGYhQev

@interface MyAnnotationClass : NSObject<MKAnnotation>

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithCoordinateCLLocationCoordinate2D) coordinate;

@end


MyAnnotationClass.m

#import "MyAnnotationClass.h"

@implementation MyAnnotationClass

-(id) initWithCoordinateCLLocationCoordinate2D) coordinate{
    self=[super init];
    if(self){
        _coordinate = coordinate;
    }
    return self;
}

-(void) dealloc{
    [_title release];
    [_subtitle release];
    [super dealloc];
}
@end


ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController<MKMapViewDelegate> {
    IBOutlet MKMapView *_myMapView;
    NSArray *_myAnnotations;
}

@property (nonatomic, retain) NSArray *myAnnotations;
@property (nonatomic, retain) IBOutlet MKMapView *myMapView;

@end


ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "laceMark.h"  
#import "MyAnnotationClass.h"
@interface ViewController ()

@end

@implementation ViewController
@synthesize myMapView = _myMapView;
@synthesize myAnnotations = _myAnnotations;

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) viewDidLoad{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

    //Initialize annotation
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
    commuterLotAnnotation.title = @"Hello title";
    commuterLotAnnotation.subtitle = @"Correct";

    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
    overflowLotAnnotation.title = @"Hello title";
    overflowLotAnnotation.subtitle = @"Correct";

    //Add them to array
    self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];

    //Release the annotations now that they've been added to the array
    [commuterLotAnnotation release];
    [overflowLotAnnotation release];

    //add array of annotations to map
    [_myMapView addAnnotations:_myAnnotations];
}


-(MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid)annotation{
    static NSString *parkingAnnotationIdentifier=@"arkingAnnotationIdentifier";

    if([annotation isKindOfClass:[MyAnnotationClass class]]){
        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView=[_myMapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];

        //If one isn't available, create a new one
        if(!annotationView){
            annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
            //Here's where the magic happens
            annotationView.image=[UIImage imageNamed"apple.gif"];
            annotationView.canShowCallout = YES;
        }
        return annotationView;
    }
    return nil;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

关于ios - 无法在 MKPinAnnotationView 上设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905274/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap