次のように、マップ上にいくつかのピンを表示しています。
for (int i = 0; i < points.count; i++) {
if([[[points objectAtIndex:i] objectForKey:@"lat"] class] != [NSNull class]) {
southWest.latitude = MAX(southWest.latitude , [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
southWest.longitude = MIN(southWest.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);
northEast.latitude = MIN(northEast.latitude, [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
northEast.longitude = MAX(northEast.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);
pin.latitude = [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue];
pin.longitude = [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue];
CarPin *cPin = [[CarPin alloc] initWithName:[[self.brain.cars objectAtIndex:i] objectForKey:@"name"] state:[self getStateStringFor:[[[points objectAtIndex:i] objectForKey:@"state"] intValue]] coordinate:pin];
cPin.state = [[[points objectAtIndex:i] objectForKey:@"state"] intValue];
cPin.carID = [[points objectAtIndex:i] objectForKey:@"objekt_id"];
[self.mapView addAnnotation:cPin];
}
}
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
次に、各ピンの「外観」を次のように選択します。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"CarPin";
if ([annotation isKindOfClass:[CarPin class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
if (((CarPin *)annotation).state == 1) {
annotationView.pinColor = MKPinAnnotationColorGreen;
} else {
annotationView.pinColor = MKPinAnnotationColorRed;
}
return annotationView;
}
return nil;
}
デリゲートも設定しました
[self.mapView setDelegate:self];
しかし、各ピンをクリックすると、画面にバブルが表示されず、詳細が表示されません! 誰かが同じことを経験していますか?