显示自定义对象的MKAnnotation

时间:2012-11-13 02:34:16

标签: ios mapkit mkannotation

我有一个对象列表,每个对象都有标题,名称,描述,纬度,经度和地址。 是否可以将此对象显示为MKAnnotations?我已经坚持了好几个小时了。当我试图使对象具有CLLocationCoordinate2D时,我不断得到关于纬度或经度不可分配的错误。

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

@interface Oficina : NSObject <MKMapViewDelegate>

@property (nonatomic, readwrite) CLLocationCoordinate2D oficinaCoordinate;
@property (nonatomic, strong) NSString *oficinaCiudad;
@property (nonatomic, strong) NSString *oficinaEstado;
@property (nonatomic, strong) NSString *oficinaTitulo;
@property (nonatomic, strong) NSString *oficinaTelefono;
@property (nonatomic, strong) NSString *oficinaLatitud;
@property (nonatomic, strong) NSString *oficinaLongitud;
@property (nonatomic, strong) NSString *oficinaID;
@property (nonatomic, strong) NSString *oficinaDireccion;
@property (nonatomic, strong) NSString *oficinaHorario;
@property (nonatomic, strong) NSString *oficinaTipoDeOficina;
@property (nonatomic, strong) NSString *oficinaServicios;
@property (nonatomic, strong) NSString *oficinaTipoDeModulo;


@end

因此,在使用互联网服务后,我得到了大约70个这样的对象。现在我希望能够将每个转换为地图注释。 这是我尝试分配纬度的方法之一,但我收到错误“Expression not assignable”..

currentOffice.oficinaCoordinate.latitude = [parseCharacters floatValue];

其中currentOffice是我的自定义对象的实例。

4 个答案:

答案 0 :(得分:2)

可以将标题,名称,描述,纬度,经度和地址显示为MKAnnotations。

您是否尝试将坐标属性更改为“assign”?

@interface MyAnnotation : NSObject<MKAnnotation> 
{   
 CLLocationCoordinate2D coordinate;
 NSString *title;
 NSString *name;
 NSString *description;
}
@property (nonatomic, assign) CLLocationCoordinate2D    coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *description;

答案 1 :(得分:2)

以下是我如何解决这个问题的示例代码。首先加载mapView注释数组:

        CLLocationCoordinate2D location;
        Annotation *myAnn;

        if (!self.locationsMutableArray) self.locationsMutableArray = [NSMutableArray array];

        NSMutableArray *tmpMutableArray = [NSMutableArray array];
        for (NSDictionary *subArr in arr)
        {
                    myAnn = [[Annotation alloc] init];
                    myAnn.address = [NSString stringWithFormat:@"%@, %@, %@, %@",
                                     [subArr objectForKey:@"address"],
                                     [subArr objectForKey:@"city"],
                                     [subArr objectForKey:@"state"],
                                     [subArr objectForKey:@"zip"]];
                    location.latitude = [[subArr objectForKey:@"lat"] doubleValue];
                    location.longitude = [[subArr objectForKey:@"lon"] doubleValue];
                    myAnn.coordinate = location;
                    myAnn.title = [subArr objectForKey:@"name"];
                    myAnn.subtitle = [subArr objectForKey:@"siteSpecialtyCategory"];
                    myAnn.siteType = [subArr objectForKey:@"siteType"];
                    myAnn.phoneNumber = [subArr objectForKey:@"phoneNumber"];
                    [tmpMutableArray addObject:myAnn];
         }
        [self.locationsMutableArray addObject:tmpMutableArray];

        [self.mapView addAnnotations:array];

真的是,你需要创建MKAnnotationView的自定义子类,我的名字是Annotation。它还必须符合<MKAnnotation>协议,您可以将它们作为自定义注释添加到地图视图中。

让您的Oficina标题文件以这样的方式开头:

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

@interface Oficina : MKAnnotationView <MKAnnotation>

@property (nonatomic, readwrite) CLLocationCoordinate2D oficinaCoordinate;
@property (nonatomic, strong) NSString *oficinaCiudad;
@property (nonatomic, strong) NSString *oficinaEstado;
@property (nonatomic, strong) NSString *oficinaTitulo;
@property (nonatomic, strong) NSString *oficinaTelefono;
@property (nonatomic, strong) NSString *oficinaLatitud;
@property (nonatomic, strong) NSString *oficinaLongitud;
@property (nonatomic, strong) NSString *oficinaID;
@property (nonatomic, strong) NSString *oficinaDireccion;
@property (nonatomic, strong) NSString *oficinaHorario;
@property (nonatomic, strong) NSString *oficinaTipoDeOficina;
@property (nonatomic, strong) NSString *oficinaServicios;
@property (nonatomic, strong) NSString *oficinaTipoDeModulo;


@end

答案 2 :(得分:0)

获取被点击的MKAnnotation的对象详细信息的方法是添加:

id<MKAnnotation> selectedOffice = view.annotation;

到委托方法

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { }

在我的特定情况下,我的MKAnnotation是一个名为Oficina的自定义对象,所以我只是将它传递给我的新视图控制器:

showOfficeDetail = (Oficina *)selectedOffice;

答案 3 :(得分:0)

这非常简单,只需循环对象数组并创建一个MKPointAnnotation坐标,并以您想要的格式显示标题和副标题。然后通过addAnnotation方法将其添加到地图中,图钉将显示在地图上。

for(Event *event in events){
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(event.lat, event.lon);
    MKPointAnnotation *point = [MKPointAnnotation.alloc initWithCoordinate:coordinate title:event.name subtitle:event.venue.name];
    [self.mapView addAnnotation:point];
}

如果您对默认图钉的外观或行为不满意,则可以通过实现viewForAnnotation来创建自定义图钉,但要注意,您不应该支持地图上的所有不同类型的注释只是你创建的,例如用户当前位置的注释。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if([annotation isKindOfClass:MKPointAnnotation.class]){
        MKPinAnnotationView *view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPin"];
        if(!view) {
            // create our custom pin view
            view = [MKPinAnnotationView.alloc initWithAnnotation:annotation reuseIdentifier:@"CustomPin"];
            view.canShowCallout = YES;
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }else{
            // update the previously created custom pin
            view.annotation = annotation;
        }
        return view;
    }
    else if([annotation isKindOfClass:MKUserLocation.class]){
        return nil; // use the default view for user location
    }
    return nil; // use the default view for any other annotations
}