为什么它在我的地图视图中显示最后一个引脚的标注注释类型
我正在设计一个地图视图,其中我没有删除。引脚数。我还在每个引脚上添加了自定义视图。现在的问题是,当我的地图视图添加所有引脚时,然后在地图上它不会显示我的最后一个引脚。它不会显示最后一个图钉,而是仅显示自定义视图,当我点击该视图时,该视图不会隐藏。在某些引脚上它不添加自定义视图。我在下面显示我的代码... class.h
@interface MappingBusinesses : UIViewController {
MKMapView *_mapView;
CalloutMapAnnotation *_calloutAnnotation;
MKAnnotationView *_selectedAnnotationView;
Places *Place_Object;
DisplayMap *dm;
NSMutableArray *routepoints;
CSMapRouteLayerView *routeView;
UICRouteOverlayMapView *routeOverlayView;
UICGDirections *diretions;
Yelp_OnTheWayAppDelegate *appDelegate;
NSArray *wayPoints;
UICGTravelModes travelMode;
DetailView *DV_Object;
UIActivityIndicatorView *aiv;
UILabel *label;
UIView *prosView;
BusinessData *bd_object;
}
class.m
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title=@"Map";
//[NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil];
label.text=[NSString stringWithFormat:@"%d places along your route",[appDelegate.busines_Aray count]];
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = appDelegate.start_lat;
region.center.longitude = appDelegate.start_long;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
dm=[[DisplayMap alloc]init];
for(int i=0;i<[appDelegate.mapPin_aray count];i++)
{
CLLocationCoordinate2D coord;
coord.latitude=[[appDelegate.mapPin_lat objectAtIndex:i] floatValue];
coord.longitude=[[appDelegate.mapPin_long objectAtIndex:i] floatValue];
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:coord.latitude andLongitude:coord.longitude] ;
[self.mapView addAnnotation:self.calloutAnnotation];
NSLog(@"coordinate of number%i is %f and %f",i,coord.latitude,coord.longitude);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *identifier = @"RoutePinAnnotation";
if ([annotation isKindOfClass:[UICRouteAnnotation class]]) {
MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(!pinAnnotation) {
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeStart) {
pinAnnotation.pinColor = MKPinAnnotationColorGreen;
} else if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeEnd) {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
} else {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
}
pinAnnotation.animatesDrop = YES;
pinAnnotation.enabled = YES;
pinAnnotation.canShowCallout = YES;
return pinAnnotation;
}
static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
if (annotation == self.calloutAnnotation) {
CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!calloutMapAnnotationView) {
calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 70.0f;
bd_object=[appDelegate.busines_Aray objectAtIndex:self.calloutAnnotation.pinID];
UITextField *txtname=[[UITextField alloc] initWithFrame:CGRectMake(0, 5, 150, 30)];
txtname.text=[NSString stringWithFormat:@"%@",bd_object.name_business ];
txtname.userInteractionEnabled=NO;
txtname.font = [UIFont boldSystemFontOfSize:18];
txtname.font=[UIFont fontWithName:@"Arial" size:18];
txtname.textColor = [UIColor whiteColor];
txtname.opaque = false;
txtname.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtname];
[txtname release];
UILabel *gpsCoordinatesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 70, 20)];
gpsCoordinatesLabel.text = @"Categories:";
gpsCoordinatesLabel.font = [UIFont boldSystemFontOfSize:14];
gpsCoordinatesLabel.font=[UIFont fontWithName:@"Arial" size:14];
gpsCoordinatesLabel.textColor = [UIColor whiteColor];
gpsCoordinatesLabel.opaque = false;
gpsCoordinatesLabel.backgroundColor = [UIColor clearColor];
gpsCoordinatesLabel.textAlignment = UITextAlignmentRight;
[calloutMapAnnotationView.contentView addSubview:gpsCoordinatesLabel];
[gpsCoordinatesLabel release];
UITextField *txtcate=[[UITextField alloc] initWithFrame:CGRectMake(70, 40, 100, 30)];
txtcate.text=[NSString stringWithFormat:@"%@", bd_object.cat_business];
txtcate.userInteractionEnabled=NO;
txtcate.font = [UIFont boldSystemFontOfSize:18];
txtcate.font=[UIFont fontWithName:@"Arial" size:18];
txtcate.textColor = [UIColor whiteColor];
txtcate.opaque = false;
txtcate.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtcate];
[txtcate release];
UITextField *txtReview=[[UITextField alloc] initWithFrame:CGRectMake(190, 40, 40, 30)];
txtReview.text=[NSString stringWithFormat:@"%d", bd_object.noofreview];
txtReview.userInteractionEnabled=NO;
txtReview.font = [UIFont boldSystemFontOfSize:18];
txtReview.textColor = [UIColor whiteColor];
txtReview.opaque = false;
txtReview.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtReview];
[txtReview release];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 31, 23)];
[imgView setImage:[UIImage imageNamed:@"reviewwhite.png"]];
[calloutMapAnnotationView.contentView addSubview:imgView];
[imgView release];
}
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
}
else {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
MKAnnotationView *av;
return av;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
[self.mapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
[self.mapView removeAnnotation: self.calloutAnnotation];
}
}
I am designing a map view in which i am dropping no. of pins. I have also add a custom view on every pin. Now problem is that when my map view add all pins then on map it don't show my last pin. Instead of last pin it will show only custom view which is not hidden when i tap on that. And on some pins it not add custom view. I am showing my code below...
class.h
@interface MappingBusinesses : UIViewController {
MKMapView *_mapView;
CalloutMapAnnotation *_calloutAnnotation;
MKAnnotationView *_selectedAnnotationView;
Places *Place_Object;
DisplayMap *dm;
NSMutableArray *routepoints;
CSMapRouteLayerView *routeView;
UICRouteOverlayMapView *routeOverlayView;
UICGDirections *diretions;
Yelp_OnTheWayAppDelegate *appDelegate;
NSArray *wayPoints;
UICGTravelModes travelMode;
DetailView *DV_Object;
UIActivityIndicatorView *aiv;
UILabel *label;
UIView *prosView;
BusinessData *bd_object;
}
class.m
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title=@"Map";
//[NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil];
label.text=[NSString stringWithFormat:@"%d places along your route",[appDelegate.busines_Aray count]];
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = appDelegate.start_lat;
region.center.longitude = appDelegate.start_long;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
dm=[[DisplayMap alloc]init];
for(int i=0;i<[appDelegate.mapPin_aray count];i++)
{
CLLocationCoordinate2D coord;
coord.latitude=[[appDelegate.mapPin_lat objectAtIndex:i] floatValue];
coord.longitude=[[appDelegate.mapPin_long objectAtIndex:i] floatValue];
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:coord.latitude andLongitude:coord.longitude] ;
[self.mapView addAnnotation:self.calloutAnnotation];
NSLog(@"coordinate of number%i is %f and %f",i,coord.latitude,coord.longitude);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *identifier = @"RoutePinAnnotation";
if ([annotation isKindOfClass:[UICRouteAnnotation class]]) {
MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(!pinAnnotation) {
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeStart) {
pinAnnotation.pinColor = MKPinAnnotationColorGreen;
} else if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeEnd) {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
} else {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
}
pinAnnotation.animatesDrop = YES;
pinAnnotation.enabled = YES;
pinAnnotation.canShowCallout = YES;
return pinAnnotation;
}
static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
if (annotation == self.calloutAnnotation) {
CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!calloutMapAnnotationView) {
calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 70.0f;
bd_object=[appDelegate.busines_Aray objectAtIndex:self.calloutAnnotation.pinID];
UITextField *txtname=[[UITextField alloc] initWithFrame:CGRectMake(0, 5, 150, 30)];
txtname.text=[NSString stringWithFormat:@"%@",bd_object.name_business ];
txtname.userInteractionEnabled=NO;
txtname.font = [UIFont boldSystemFontOfSize:18];
txtname.font=[UIFont fontWithName:@"Arial" size:18];
txtname.textColor = [UIColor whiteColor];
txtname.opaque = false;
txtname.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtname];
[txtname release];
UILabel *gpsCoordinatesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 70, 20)];
gpsCoordinatesLabel.text = @"Categories:";
gpsCoordinatesLabel.font = [UIFont boldSystemFontOfSize:14];
gpsCoordinatesLabel.font=[UIFont fontWithName:@"Arial" size:14];
gpsCoordinatesLabel.textColor = [UIColor whiteColor];
gpsCoordinatesLabel.opaque = false;
gpsCoordinatesLabel.backgroundColor = [UIColor clearColor];
gpsCoordinatesLabel.textAlignment = UITextAlignmentRight;
[calloutMapAnnotationView.contentView addSubview:gpsCoordinatesLabel];
[gpsCoordinatesLabel release];
UITextField *txtcate=[[UITextField alloc] initWithFrame:CGRectMake(70, 40, 100, 30)];
txtcate.text=[NSString stringWithFormat:@"%@", bd_object.cat_business];
txtcate.userInteractionEnabled=NO;
txtcate.font = [UIFont boldSystemFontOfSize:18];
txtcate.font=[UIFont fontWithName:@"Arial" size:18];
txtcate.textColor = [UIColor whiteColor];
txtcate.opaque = false;
txtcate.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtcate];
[txtcate release];
UITextField *txtReview=[[UITextField alloc] initWithFrame:CGRectMake(190, 40, 40, 30)];
txtReview.text=[NSString stringWithFormat:@"%d", bd_object.noofreview];
txtReview.userInteractionEnabled=NO;
txtReview.font = [UIFont boldSystemFontOfSize:18];
txtReview.textColor = [UIColor whiteColor];
txtReview.opaque = false;
txtReview.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtReview];
[txtReview release];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 31, 23)];
[imgView setImage:[UIImage imageNamed:@"reviewwhite.png"]];
[calloutMapAnnotationView.contentView addSubview:imgView];
[imgView release];
}
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
}
else {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
MKAnnotationView *av;
return av;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
[self.mapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
[self.mapView removeAnnotation: self.calloutAnnotation];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定,但我认为问题可能是在 viewDidLoad 的初始循环期间,您记住了 self.calloutAnnotation 中的注释,然后在 didSelect 和 didDeselect 中检查它。
问题是 self.calloutAnnotation 每次都会在 init 循环中被替换,所以最后,它只会引用最后添加的注释。例如,当用户选择不同的注释时,您可以更改 self.calloutAnnotation 的纬度/经度并再次添加它,但这将更改数组中添加的最后一个注释的纬度/经度。
Not sure, but I think the issue might be that during your initial loop in viewDidLoad, you're remembering the annotations in self.calloutAnnotation and then checking for it in didSelect and didDeselect.
The problem is that self.calloutAnnotation will be replaced every time round the init loop, so at the end, it will only be referencing the last annotation added. For example, when user selects a different annotation, you change self.calloutAnnotation's lat/long and add it again, but that will change the lat/long of the last annotation added in the array.