Problem in Mapkit
I am using following code to show location of the objects.
- (void)viewDidLoad {
[super viewDidLoad];
[self selectData];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate1 = [location coordinate];
//NSString *latitude = [NSString stringWithFormat:@"%f", coordinate1.latitude];
//NSString *longitude = [NSString stringWithFormat:@"%f", coordinate1.longitude];
mapView.showsUserLocation=YES;
mapView.delegate=self;
//MKMapView *mapView = (MKMapView*)self.view;
CLLocationCoordinate2D coordinate;
coordinate.latitude = coordinate1.latitude;
coordinate.longitude =coordinate1.longitude;
//mapView.region = ;
[mapView setRegion:MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000)];
NSUInteger count = 1;
[self setTitle:[NSString stringWithFormat:@"%i Memories",[memoryArray count]]];
for(int i = 0; i < [memoryArray count]; i++) {
MemoryData *m=[memoryArray objectAtIndex:i];
//CGFloat latDelta = rand()*.035/RAND_MAX -.02;
//CGFloat longDelta = rand()*.03/RAND_MAX -.015;
coordinate.latitude=[m.lat floatValue];
coordinate.longitude=[m.lon floatValue];
CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude};
MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc]initWithCoordinate:newCoord andID:count++];
[annotation setMemoryData:m];
[annotation setTag:i];
[mapView addAnnotation:annotation];
[annotation release];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil) {
p开发者_运维技巧inView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
pinView.pinColor = MKPinAnnotationColorRed;
int t=((MapDemoAnnotation*)annotation).tag ;
NSLog(@"Tag is %i ",t);
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIImageView *pinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 0, 34, 34)];
UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"photo" ofType:@"JPG"]];
pinImageView.image = pinImage;
//[pinImage release];
//[pinView addSubview:pinImageView];
//[pinImageView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
btn.tag = t;
pinView.rightCalloutAccessoryView = btn;
//pinView.rightCalloutAccessoryView=pinImageView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
ViewItemList *list=[[ViewItemList alloc]init];
MemoryData *m=[memoryArray objectAtIndex:control.tag];
[list setMemoryData:m];
[self.navigationController pushViewController:list animated:YES];
}
If I am testing on ipod it works fine. but it i test on iphone or on simulator it shows following error .
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocation tag]: unrecognized selector sent to instance 0xac20da0'
Please help
It seems the problem is that your custom class MapDemoAnnotation
is not a subclass of UIView
which implements the tag
property. According to your error, it thinks it is a subclass of MKUserLocation
.
If you classes are OK in this respect, most likely your cast is not working. Maybe you are over-using the parentheses? Try writing the line like this:
MapDemoAnnotation *myDemo = (MapDemoAnnotation *) annotation;
int t = myDemo.tag;
精彩评论