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

Azh*_*har 2 iphone objective-c mapkit ipad ios

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

我用谷歌搜索但是我发现MapKit得到一个图像并在开始和结束点设置它,找不到第二个图像的任何帮助.

喜欢

annotationView.image=[UIImage imageNamed:@"parkingIcon.png"];
Run Code Online (Sandbox Code Playgroud)

它只为起点和终点设置了一个图像.但是我想为这两点添加不同的图像.

请帮忙.

Azh*_*har 8

我得到了...感谢所有试图帮助我的人.完整的解决方案是

创建一个类

 @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;
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

这就是为MapKit上的每个点添加单独图像的方法.