Ios 显示自定义对象的注释

Ios 显示自定义对象的注释,ios,mapkit,mkannotation,Ios,Mapkit,Mkannotation,我有一个对象列表,每个对象都有标题、名称、描述、纬度、经度和地址。 是否可以将此对象显示为注释?这件事我已经坚持了好几个小时了。当我试图使对象具有CLLocationCoordinate2D时,我不断得到关于纬度或经度不可分配的错误 #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface Oficina : NSObject <MKMapViewDelegate> @pr

我有一个对象列表,每个对象都有标题、名称、描述、纬度、经度和地址。 是否可以将此对象显示为注释?这件事我已经坚持了好几个小时了。当我试图使对象具有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

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

可以将标题、名称、描述、纬度、经度和地址显示为注释

是否尝试将坐标属性更改为“指定”

@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;
@接口MyAnnotation:NSObject
{   
CLLocationCoordinate2D坐标;
NSString*标题;
NSString*名称;
NSString*说明;
}
@属性(非原子,赋值)CLLocationCoordinate2D坐标;
@属性(非原子,副本)NSString*标题;
@属性(非原子,副本)NSString*名称;
@属性(非原子,副本)NSString*说明;

下面是我如何解决这个问题的一些示例代码。首先加载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
mine的自定义子类
Annotation
。它还必须符合
协议,您可以将它们作为自定义注释添加到地图视图中

使您的
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
#导入
#进口
@Cina的界面:MKAnnotationView
@Cinacoordina坐标的性质(非原子,读写)ClLocationCoordinated2D;
@Cinaciudad的属性(非原子,强)NSString*;
@性质(非原子,强)非七叶皂苷的NSString*;
@CINATITULO的性质(非原子,强)NSString*;
@性质(非原子,强)NSString*oficinaTelefono;
@Cinalatitud的属性(非原子,强)NSString*;
@CINALONGITUD的属性(非原子,强)NSString*;
@性质(非原子,强)NSString*oficinaID;
@属性(非原子,强)NSString*OFICINADIRECION;
@辛纳胡里奥的性质(非原子,强)NSString*;
@Cinatipodeoficina的性质(非原子,强)NSString*;
@CINASERVICIOS的属性(非原子,强)NSString*;
@CINATIPODEMODULO的性质(非原子,强)NSString*;
@结束

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

id<MKAnnotation> selectedOffice = view.annotation;
在我的特定案例中,我的MKAnnotation是一个名为Oficina的自定义对象,因此我将其传递给了我的新视图控制器,如下所示:

showOfficeDetail = (Oficina *)selectedOffice;

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

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];
}
如果您对默认pin的外观或行为不满意,则可以通过实现
viewForAnnotation
来创建自定义pin,但必须注意,您支持地图上所有不同类型的注释,而不仅仅是您创建的注释,例如用户当前位置的注释

- (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
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
if([annotation iskindof类:MKPointAnnotation.class]){
MKPinAnnotationView*视图=(MKPinAnnotationView*)[mapView出列重用AnnotationViewWithIdentifier:@“CustomPin”];
如果(!视图){
//创建自定义pin视图
视图=[MKPinAnnotationView.alloc initWithAnnotation:annotation重用标识符:@“CustomPin”];
view.canShowCallout=是;
view.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypedTailDisclosure];
}否则{
//更新以前创建的自定义pin
view.annotation=注释;
}
返回视图;
}
else if([annotation isKindOfClass:MKUserLocation.class]){
返回nil;//使用用户位置的默认视图
}
返回nil;//对任何其他注释使用默认视图
}

您可以发布自定义对象的代码吗?可能的话,我之前已经发布了,请给我一秒钟时间查看我的代码并向您展示一个示例。好的,现在我的对象是MKAnnotationView的子类,并且符合MKAnnotation…我可以设置Oficinacoordina的纬度和经度。所以现在我要处理70个信息正确的对象。我错过了什么才能把它们放到屏幕上?你是在问当他们点击你的注释时如何在屏幕上显示它们吗?太好了!我很高兴我能帮上忙:)嗨,我的应用程序中的地图部分还在苦苦挣扎。最后,我展示了带有自定义图标和rightCalloutAccessoryView的MKAnnotation。由于注释包含我的店铺(办公室)的所有信息,因此我希望在用户单击RightCallout AccessoryView时显示该详细信息。由于某些原因,我看不到如何传递我的对象,因此当detailViewController显示时,它会在屏幕上显示信息。有什么想法吗?我正在使用mapVies:annotationView:calloutAccessoryControlTapped来响应单击。但无法传递我的自定义对象。让我们再问一个问题。所以我能把所有的针都放在正确的地方。。。但是,假设我的应用程序打开时会放大到当前位置。。。然后它就掉了别针。如果最近的pin不在屏幕范围内怎么办?是否可以轻松地重新设置范围,使至少一个管脚可见?
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];
}
- (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
}