我很难弄清楚我在这里做错了什么......我正在通过循环向我的地图视图添加注释,但注释图像每次都是完全随机NSLog
的......当我订购时,它是随机的 - 我'我不确定这是否是问题所在。
- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([[annotation subtitle] isEqual:@"Bar"]) {
MKAnnotationView *view = nil;
view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
if (!view) {
// Could not reuse a view ...
// Creating a new annotation view
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
view.enabled = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.image = [UIImage imageNamed:@"beer.png"];
}
return view;
}
else if ([[annotation subtitle] isEqual:@"Club"]) {
MKAnnotationView *view = nil;
view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
if (!view) {
// Could not reuse a view ...
// Creating a new annotation view
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
view.enabled = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.image = [UIImage imageNamed:@"clubs.png"];
}
return view;
}
}
view.image 是完全随机的......要么clubs.png
或beer.png
.. 我如何正确地制作它?
这就是我添加注释的方式:
- (void)barLoop {
for (int i = 0; i<barArray.count; i++) {
int index = [[barArray objectAtIndex:i]intValue];
NSString *lati = [[[self.usersLocationArray objectAtIndex:index]
valueForKeyPath:@"items.Latitude"]componentsJoinedByString:@""];
NSString *longi = [[[self.usersLocationArray objectAtIndex:index]
valueForKeyPath:@"items.Longitude"]componentsJoinedByString:@""];
NSString *barNavn = [[[self.usersLocationArray objectAtIndex:index] valueForKeyPath:@"items.Navn"]componentsJoinedByString:@""];
float latitude = [lati floatValue];
float longitude = [longi floatValue];
MKCoordinateRegion region = { {0.0, 0.0} , {0.0, 0.0} };
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.longitudeDelta = 0.20f;
region.span.latitudeDelta = 0.20f;
[mapView setRegion:region animated:NO];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
Annotation *ann = [[Annotation alloc]initWithPosition:location];
ann.title = barNavn;
ann.subtitle = @"Bar";
[self.mapView addAnnotation:ann];
}
}
提前致谢 :)