为什么无法在mapView上添加注释???我得到带注释的数组,然后尝试将它们放到map(((map是空的(这是我的代码和viewAnnotation方法,我发现我自己的注释类错误了(
@implimentation MyClass {
NSMutableArray *_annotation;
}
_agents = [[Database sharedDatabase] agentsList];
_mapView.delegate = self;
[_agents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[_annotations addObject:[[[BronAgentMapViewAnnotation alloc] initWithAgency:obj] autorelease]];
}];
NSLog(@"num of annot = %d", _annotations.count); //num of annot = 120
_mapView.showsUserLocation = YES;
if (_showUserLocation) {
_mapView.showsUserLocation = YES;
}
[_annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[_mapView addAnnotation: obj];
}];
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_annotations = [[NSMutableArray array] retain];
}
return self;
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// in case it's the user location, we already have an annotation, so just return nil
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// handle our three custom annotations
//
if ([annotation isKindOfClass:[BronAgentMapViewAnnotation class]])
{
// try to dequeue an existing pin view first
static NSString *bronAgentMapViewAnnotationIdentifier = @"BronAgentMapViewAnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)
[_mapView dequeueReusableAnnotationViewWithIdentifier:bronAgentMapViewAnnotationIdentifier];
if (pinView == nil)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:bronAgentMapViewAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
//
// note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
// - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
//
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
rightButton.tag = [(BronAgentMapViewAnnotation *) annotation annotationId];
customPinView.hidden = [(BronAgentMapViewAnnotation *) annotation isHidden];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.rightCalloutAccessoryView.tag = [(BronAgentMapViewAnnotation *) annotation annotationId];
pinView.hidden = [(BronAgentMapViewAnnotation *) annotation isHidden];
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
可以帮忙吗?
答案 0 :(得分:0)
您需要实现地图委托。注释视图在那里分配。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
答案 1 :(得分:0)
尝试创建自己的annotationView类并调用viewforannotation,如下所示:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[yourAnnotationClass class]]) {
yourAnnotationView *annotationView = (yourAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];
if (!annotationView)
annotationView = [[yourAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation" delegate:self];
else
annotationView.annotation = annotation;
return annotationView;
}
return [_mapView viewForAnnotation:annotation];
}
答案 2 :(得分:0)
您是否已将_mapView IBOutlet连接到界面中的MKMapView?如果没有它,它不知道它在屏幕上绘制的MKMapView是你正在调用_mapView并添加注释的那个,所以它不会要求绘制那些注释。
答案 3 :(得分:0)
使用此:
mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0,0,320,460)];
mapView.userInteractionEnabled=YES;
mapView.zoomEnabled=YES;
mapView.delegate=self;
[self.view addSubview:mapView];
[self annotationPlacing];
// 36.777313,-119.706405 36.120838,-115.33682 47.798397,-121.519775 47.606337,-122.330618
}
-(void)annotationPlacing
{
NSArray *arrLat=[[NSArray alloc]initWithObjects:@"36.777313",@"36.120838",@"47.798397",@"47.606337", nil];
NSArray *arrLong=[[NSArray alloc]initWithObjects:@"-119.706405",@"-115.33682",@"-121.519775",@"-122.330618", nil];
for(int i=0;i<4;i++)
{
CLLocationCoordinate2D location;
location.latitude=[[arrLat objectAtIndex:i]floatValue];
location.longitude=[[arrLong objectAtIndex:i]floatValue];
MKPointAnnotation *annottion=[[MKPointAnnotation alloc]init];
annottion.coordinate=location;
annottion.title=@"India";
annottion.subtitle=@"Hyderabad";
[arrAnnotation addObject:annottion];
[mapView addAnnotation:annottion];
}
}