将NSMutable数组导入地图注释

时间:2013-04-22 16:17:08

标签: annotations nsmutablearray mapkit

大家好我有一个由7列组成的mysql数据库,我有一个脚本读取它并将数据提取为JSON我已经拉入NSMutableArray,我希望能够使用此数组来设置注释我的地图,但我不知道该怎么做,因为你可以看到我在这里定义了一个注释,显示没有问题,但我真的不知道如何显示NSMutableArray项目? NSMutable数组将提供比Annotations更多的信息,我只需要,坐标,标题和副标题,那么我该怎么做呢?到目前为止,这是我的代码:

hazards.h

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

@interface Hazards : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@property (nonatomic, strong) NSString * ID;
@property (nonatomic, strong) NSString * ROUTE;
@property (nonatomic, strong) NSString * ADDRESS;
@property (nonatomic, strong) NSString * LATITUDE;
@property (nonatomic, strong) NSString * LONGITUDE;
@property (nonatomic, strong) NSString * HAZARD;
@property (nonatomic, strong) NSString * RISK;

// Methods
- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK;

@end

hazards.m

#import "Hazards.h"

@implementation Hazards
@synthesize coordinate, title, subtitle, ID, ROUTE, ADDRESS, LATITUDE, LONGITUDE, HAZARD, RISK;

- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK {

    self = [super init];
    if (self)
    {
        ID = hazardsID;
        ROUTE = hazardsROUTE;
        ADDRESS = hazardsADDRESS;
        LATITUDE = hazardsLATITUDE;
        LONGITUDE = hazardsLONGITUDE;
        HAZARD = hazardsHAZARD;
        RISK = hazardsRISK;
    }
return self;
}

@end

viewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "Hazards.h"

@interface ViewController : UIViewController

@property (nonatomic, strong) NSMutableArray *json;
@property (nonatomic, strong) NSMutableArray *hazardsArray;

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

#pragma mark - Methods
-(void) retrieveData;

@end

viewController.m

#import "ViewController.h"
#import "Hazards.h"

@interface ViewController ()

@end

// Railway Street Ballymena Coordinates
#define BALLYMENA_LATITUDE 54.857719;
#define BALLYMENA_LONGITUDE -6.280654;

// Span
#define THE_SPAN 0.01f;

#define getDataURL @"localhost:8888/rmb/json.php"

@implementation ViewController
@synthesize json, hazardsArray, mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the region
    MKCoordinateRegion myRegion;

    // Center
    CLLocationCoordinate2D center;
    center.latitude = BALLYMENA_LATITUDE;
    center.longitude = BALLYMENA_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;

    // Set our mapview
    [mapView setRegion:myRegion animated: YES];

    // Annotation
    NSMutableArray *locations = [[NSMutableArray alloc] init];
    CLLocationCoordinate2D location;
    Hazards *myAnn;

    // Pin to show Royal Mail Ballymena delivery office
    myAnn = [[Hazards alloc] init];
    location.latitude = BALLYMENA_LATITUDE;
    location.longitude = BALLYMENA_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Royal Mail Ballymena";
    myAnn.subtitle = @"111, Railway Street";
    [locations addObject:myAnn];

    [self.mapView addAnnotations:locations];

    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods
-(void) retrieveData {
    NSURL *url = [NSURL URLWithString:getDataURL];
    NSData *data = [NSData dataWithContentsOfURL:url];

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    // Setup our hazards array
    hazardsArray = [[NSMutableArray alloc] init];

    for (int i =0; i < json.count; i++) {

        // Create hazard object
        NSString *hazardsID = [[json objectAtIndex:i] objectForKey:@"ID"];
        NSString *hazardsROUTE = [[json objectAtIndex:i] objectForKey:@"ROUTE"];
        NSString *hazardsADDRESS = [[json objectAtIndex:i] objectForKey:@"ADDRESS"];
        NSString *hazardsLATITUDE = [[json objectAtIndex:i] objectForKey:@"LATITUDE"];
        NSString *hazardsLONGITUDE = [[json objectAtIndex:i] objectForKey:@"LONGITUDE"];
        NSString *hazardsHAZARD = [[json objectAtIndex:i] objectForKey:@"HAZARD"];
        NSString *hazardsRISK = [[json objectAtIndex:i] objectForKey:@"RISK"];

        Hazards *myHazards = [[Hazards alloc] initWithID:hazardsID andROUTE:hazardsROUTE andADDRESS:hazardsADDRESS andLATITUDE:hazardsLATITUDE andLONGITUDE:hazardsLONGITUDE andHAZARD:hazardsHAZARD andRISK:hazardsRISK];

        // Add our hazards object to our hazards array
        [hazardsArray addObject:myHazards];
    }
    // [self.mapView addAnnotation:hazardsArray];
}

@end

非常感谢提前

1 个答案:

答案 0 :(得分:0)

假设您的retrieveData正确构建了危险物品,并且在正确的时间被调用,您可以在循环中将此行一次添加一个

[self.mapView addAnnotation:myHazards];
循环结束后

或一次性完成

[self.mapView addAnnotations:hazardsArray];