【发布时间】:2014-06-24 15:30:54
【问题描述】:
我正在构建一个 iPhone 应用程序,而我的标签一直在抓取我 plist 中的最后一项。应用程序的第一部分显示带有注释的地图。注释——纬度和经度以及标题也都是从同一个 plist 中提取的。
这是我的代码:
#import "myMapViewController.h"
#import "MapViewAnnotation.h"
@interface myMapViewController ()
@property NSArray *states;
//@property NSString *nameString;
//@property NSString *zipString;
//@property NSString *subtitleString;
//@property NSString *zipString;
@end
@implementation myMapViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.mapView.showsUserLocation = YES; //showsUserLocation = YES;
self.mapView.delegate = self;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated: YES];
[self.mapView addAnnotations:[self createAnnotations]];
//[self zoomToLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle]pathForResource:@"States" ofType:@"plist"];
NSArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
int count = rootLevel.count;
for (int i = 0; i < count; i++){
NSDictionary *secondLevel = [rootLevel objectAtIndex:i];
// NSArray *state = [secondLevel valueForKey:@"State"];
NSArray *stores = [secondLevel valueForKey:@"Stores"];
for (NSDictionary *row in stores) {
NSString *latitude = [row objectForKey:@"Latitude"];
NSString *longitude = [row objectForKey:@"Longitude"];
NSString *title = [row objectForKey:@"Name"];
NSString *nameString = [row objectForKey:@"Number"];
NSString *zipString = [row objectForKey:@"Zip"];
NSString *subtitle = [row objectForKey:@"Address1"];
//Create coordinates from the latitude and longitude values
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord AndZipString:zipString AndNameString:nameString AndSubSubtitle:subtitle];
[annotations addObject:annotation];
//set label
}
}
return annotations;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"showStoreDetails"])
{
MKAnnotationView *annotationView = sender;
id<MKAnnotation> ann = annotationView.annotation;
[segue.destinationViewController setNameString:ann.title];
[segue.destinationViewController setZipString:ann.title];
NSLog(@"log %@", annotationView.annotation.subtitle);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = @"StandardPin";
MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
if (aView == nil)
{
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
//aView.animatesDrop = YES;
}
aView.annotation = annotation;
return aView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"showStoreDetails" sender:view];
NSLog(@"accessory button tapped for annotation %@", view.annotation.title);
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.7;
span.longitudeDelta=0.7;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
@end
【问题讨论】:
-
你的意思是它只显示 plist 中的最后一个数据/记录?
-
完全跑题了,但是您是否意识到您的 METERS_PER_MILE 常量是错误的?应该是 1609.344。
-
根本不清楚您的问题和/或问题是什么。由于您的代码中没有提及标签,因此将哪个标签设置为最后一个值?
标签: ios plist uistoryboardsegue mkannotationview