1
我想表明我的当前位置的位置,并随机5分点是这样的:的MKMapView没有显示注释的点
- (void)viewDidLoad {
[super viewDidLoad];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
otherlat = [NSNumber numberWithDouble:[currentLatitude doubleValue]];
otherlong = [NSNumber numberWithDouble:[currentLongitude doubleValue]];
[self.mapView setShowsUserLocation:YES];
mapView.delegate = self;
longi = [[NSMutableArray alloc] init];
lati = [[NSMutableArray alloc] init];
NSMutableArray *la = [[NSMutableArray alloc] init];
NSMutableArray *lo = [[NSMutableArray alloc] init];
la = [NSMutableArray arrayWithObjects:@"20", @"21", @"42", @"51", @"75", nil];
lo = [NSMutableArray arrayWithObjects:@"60", @"21", @"82", @"181", @"35", nil];
for (int x = 0; x < [la count]; x++) {
otherlat = [NSNumber numberWithDouble:[[la objectAtIndex:x] doubleValue]];
otherlong = [NSNumber numberWithDouble:[[lo objectAtIndex:x] doubleValue]];
[longi addObject:otherlong];
[lati addObject:otherlat];
}
myAnnotation *myAnnotation1 = [[myAnnotation alloc] init];
for (int y = 0; y < [lati count]; y++) {
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [[lati objectAtIndex:y] doubleValue];
theCoordinate.longitude = [[longi objectAtIndex:y] doubleValue];
myAnnotation1.coordinate = theCoordinate;
[mapView addAnnotation:myAnnotation1];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *myAnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier];
customPinView.image = [UIImage imageNamed:@"purplepin.png"];
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
return customPinView;
}
不过,MapView的只显示我的当前位置,并且没有的其他5个地点。我不明白为什么它不显示其他5个位置,因为我做的是
[mapView addAnnotation:myAnnotation1];
关键问题,将注释分配/初始化内部'for'循环。您的经度也不能超过180.0。你可以显着简化你的数组逻辑。你的viewForAnnotation'应该使用'customPinView.pinColor = MKPinAnnotationColorPurple'而不是png。你的'viewForAnnotation'实际上应该在创建一个新的''dequeueReusableAnnotationViewWithIdentifier'之前尝试。如果您未在注释中设置“标题”属性等,则无需显示标注。 – Rob
谢谢您的建议 – Alexyuiop