【问题标题】:Showing a MKAnnotation for custom object显示自定义对象的 MKAnnotation
【发布时间】:2012-11-13 02:34:16
【问题描述】:

我有一个对象列表,每个对象都有标题、名称、描述、纬度、经度和地址。 是否可以将此对象显示为 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 个这样的对象。现在我希望能够将其中的每一个都变成地图注释。 这是我尝试分配纬度的方法之一,但我收到错误“表达式不可分配”..

currentOffice.oficinaCoordinate.latitude = [parseCharacters floatValue];

currentOffice 是我的自定义对象的一个​​实例。

【问题讨论】:

  • 您可以发布自定义对象的代码吗?
  • 有可能,我已经做到了,然后再给我看一下我的代码并给你看一个例子。

标签: ios mapkit mkannotation


【解决方案1】:

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

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

@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;

【讨论】:

  • 哦,还有一个简单的问题。所以我能够将所有图钉放在正确的位置......但是,假设我的应用程序在打开时会放大到当前位置......然后它会丢掉图钉。如果最近的引脚不在屏幕范围内怎么办?重新设置范围是否容易让至少一个引脚可见?
【解决方案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。它还必须符合&lt;MKAnnotation&gt; 协议,您可以将它们作为自定义注释添加到您的地图视图中。

让你的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

【讨论】:

  • 好的,现在我的对象是 MKAnnotationView 的一个子类,并且符合 MKAnnotation...我能够设置 oficinaCoordinate 的纬度和经度。所以现在我被 70 个具有正确信息的对象困住了。我错过了什么才能将它们放在屏幕上?
  • 当他们点击您的注释时,您是否在问如何在屏幕上显示它们?
  • 太棒了!我很高兴能帮上忙:)
  • 嗨,我还在为我的应用程序的这个地图部分而苦苦挣扎。所以最后我展示了带有自定义图标和 rightCalloutAccessoryView 的 MKAnnotation。由于注释包含我的商店(办公室)的所有信息,我想在用户单击 rightCalloutAccessoryView 时显示该详细信息。出于某种原因,我看不到如何传递我的对象,因此当 detailViewController 出现时,它会在屏幕上显示信息。有任何想法吗?我正在使用 mapVies:annotationView:calloutAccessoryControlTapped 来响应点击。但无法传递我的自定义对象。
【解决方案3】:

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

id<MKAnnotation> selectedOffice = view.annotation;

委托方法

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

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

showOfficeDetail = (Oficina *)selectedOffice;

【讨论】:

    【解决方案4】:

    这非常简单,只需循环您的对象数组并创建一个带有坐标的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];
    }
    

    如果您对默认 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多