大家好,我有一个由 7 列组成的 mysql 数据库,我有一个脚本可以读取它并将数据提取为 JSON,我已将其提取到 NSMutableArray 中,我希望能够使用此数组在我的地图上设置注释但是我不知道在这里做什么,正如你所看到的,我在这里定义了一个注释,它显示没有问题,但老实说我不确定如何显示 NSMutableArray 项目?NSMutable 数组将包含比注释所需的更多信息,我只需要坐标、标题和副标题,那么我该怎么做呢?到目前为止,这是我的代码:
危害.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
危害.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
视图控制器.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
视图控制器.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
提前谢谢了