2

我有一个对象列表,每个对象都有标题、名称、描述、纬度、经度和地址。是否可以将此对象显示为 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 是我的自定义对象的一个​​实例。

4

4 回答 4

2

可以将标题、名称、描述、纬度、经度和地址显示为 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;
于 2012-11-13T04:43:33.333 回答
1

这是我如何解决这个问题的一些示例代码。首先加载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
于 2012-11-13T04:44:59.430 回答
0

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

id<MKAnnotation> selectedOffice = view.annotation;

到委托方法

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

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

showOfficeDetail = (Oficina *)selectedOffice;
于 2012-11-14T23:42:54.333 回答
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
}
于 2020-06-16T10:11:22.340 回答