将数组中的位置绘制为多个注释

时间:2014-10-28 21:47:53

标签: ios arrays json mapkit mkannotation

我是ios编程的新手,我正在尝试在mapview上绘制多个位置作为注释。

我能够做的最接近的事情是从服务器获取位置并将它们放在桌子上,然后通过单击我想要查看的位置将它们单独放在桌子上。

我的问题是:我已将数组中的json元素作为位置显示如下,但我无法将地图上的位置绘制为多个注释,因此我试图跳过必须使用表格和从数组转到mapview。任何帮助将不胜感激,谢谢。

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

@protocol HomeModelProtocol <NSObject>

- (void)itemsDownloaded:(NSArray *)items;

@end

@interface HomeModel : NSObject <NSURLConnectionDataDelegate>

@property (nonatomic, weak) id<HomeModelProtocol> delegate;

- (void)downloadItems;

@end

这是下载位置并放入数组的类

// HomeModel.m

#import "HomeModel.h"
#import "Location.h"

@interface HomeModel()
{
   NSMutableData *_downloadedData;
}
@end

@implementation HomeModel

- (void)downloadItems
{
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://random.name/service.php"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse         *)response
{
_downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableArray *_locations = [[NSMutableArray alloc] init];

NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData   options:NSJSONReadingAllowFragments error:&error];

// Loop through Json objects, create question objects and add them to questions array
for (int i = 0; i < jsonArray.count; i++)
{
    NSDictionary *jsonElement = jsonArray[i];

    Location *newLocation = [[Location alloc] init];
    newLocation.name = jsonElement[@"Name"];
    newLocation.address = jsonElement[@"Address"];
    newLocation.latitude = jsonElement[@"Latitude"];
    newLocation.longitude = jsonElement[@"Longitude"];

    [_locations addObject:newLocation];
}

// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
    [self.delegate itemsDownloaded:_locations];
}
}
@end

这是将数组放入表中的类

//ViewController.m

#import "ViewController.h"
#import "Location.h"
#import "DetailViewController.h"

@interface ViewController ()
{
HomeModel *_homeModel;
NSArray *_feedItems;
Location *_selectedLocation;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.listTableView.delegate = self;
self.listTableView.dataSource = self;

_feedItems = [[NSArray alloc] init];    
_homeModel = [[HomeModel alloc] init];    
_homeModel.delegate = self;    
[_homeModel downloadItems];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

-(void)itemsDownloaded:(NSArray *)items
{    
// Set the downloaded items to the array
_feedItems = items;    
[self.listTableView reloadData];
}

#pragma mark Table View Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _feedItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
// Retrieve cell
NSString *cellIdentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    
Location *item = _feedItems[indexPath.row];    
myCell.textLabel.text = item.address;    
return myCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedLocation = _feedItems[indexPath.row];    
[self performSegueWithIdentifier:@"detailSegue" sender:self];
}

#pragma mark Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;    
detailVC.selectedLocation = _selectedLocation;
}
@end

这是从表中获取结果并将其发布在地图视图上的类。

//DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D poiCoodinates;
poiCoodinates.latitude = [self.selectedLocation.latitude doubleValue];
poiCoodinates.longitude= [self.selectedLocation.longitude doubleValue];

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750);

[self.mapView setRegion:viewRegion animated:YES];

MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];
pin.coordinate = poiCoodinates;
[self.mapView addAnnotation:pin];
}

@end

1 个答案:

答案 0 :(得分:1)

您可以将多个MKPointAnnotations存储在一个数组中,并使用addAnnotations方法将该注释数组添加到mapview。

NSMutableArray* annotations = [[NSMutableArray alloc] init];

for (int i = 0; i < jsonArray.count; i++)
{
    NSDictionary *jsonElement = jsonArray[i];
    MKPointAnnotation* marker = [[MKPointAnnotation alloc] init];

    marker.title = jsonElement[@"Name"];
    marker.subtitle = jsonElement[@"Address"];
    marker.coordinate.latitude = jsonElement[@"Latitude"];
    marker.coordinate.longitude = jsonElement[@"Longitude"];
    [annotations addObject:marker];
}

[self.mapView addAnnotations:markerArray];