iphone:如何为 MapKit 上的每个针点显示不同的图像?

标签 iphone objective-c ios ipad mapkit

我想在 iPhone/iPad 应用程序中叠加开始和结束图像。我有起点和终点纬度和经度值,想在起点和终点之间绘制叠加层,并将起点图像放在起点上,将终点图像放在终点上。

我已经用谷歌搜索了,但我发现 MapKit 获取了一张图像并将其设置在起点和终点,找不到关于第二张图像的任何帮助。

喜欢

annotationView.image=[UIImage imageNamed:@"parkingIcon.png"];

它只为起点和终点设置一张图像。但我想为这两个点放置不同的图像。

请帮忙。

最佳答案

我明白了……感谢所有试图帮助我的人。完整的解决方案是

创建一个类

 @interface MyAnnotationClass : NSObject <MKAnnotation> {
        NSString *_name;
        NSString *_description;
        CLLocationCoordinate2D _coordinate;


    }
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *description;
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

    -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;

ViewDidLoad 方法代码:

mapView.delegate = self;
    //Initialize annotation
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047752, -76.850388)];
    commuterLotAnnotation.name = @"1";
    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(  39.047958, -76.852520)];
    overflowLotAnnotation.name = @"2";

    //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
    [mapView addAnnotations:_myAnnotations];

viewForAnnotation 代码:

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";

    if([annotation isKindOfClass:[MyAnnotationClass class]]){

        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
        //If one isn't available, create a new one
        if(!annotationView){
            annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
            if([((MyAnnotationClass *)annotation).name isEqualToString: @"1"]){
                annotationView.image=[UIImage imageNamed:@"passenger.png"];
            }
            else if([((MyAnnotationClass *)annotation).name isEqualToString: @"2"]){
                annotationView.image=[UIImage imageNamed:@"place.png"];
            }                                   
        }
        return annotationView;
    }
    return nil;
}

这就是您可以为 MapKit 上的每个点添加单独图像的方法。

关于iphone:如何为 MapKit 上的每个针点显示不同的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585124/

相关文章:

objective-c - 如何在 MPMoviePlayerController 中播放 webm 流视频?

ios - 逐个迭代格式 % 占位符

ios - 如何将多个标签传递给 addTarget :action:forControlEvents?

ios - 我正在尝试通过电子邮件重设密码以进行解析,但是我找不到用户的正确电子邮件ID,并且我正在使用此代码

iphone - 以编程方式加载 View Controller 的 Outlet View

ios - 我如何获得有关已在我的 SpriteKit 游戏中完成关卡的用户数量或百分比的统计信息?

ios - 以编程方式添加 socket

iphone - 隐藏后navigationController工具栏不会重新出现

objective-c - 找到最近的CCSprite

objective-c - GCD Dispatch - 调用其他方法时发送通知