【问题标题】:MKMapView -> display a button for my locationMKMapView -> 为我的位置显示一个按钮
【发布时间】:2011-01-05 04:47:34
【问题描述】:

我有一个MKMapView 用这些源代码行实现(我的位置是蓝色子弹,另一个是紫色针):

- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == mapViewLocal.userLocation) {
        mapViewLocal.userLocation.title = @"Test";
        [mapViewLocal setRegion:MKCoordinateRegionMakeWithDistance(mapViewLocal.userLocation.coordinate, 1000, 1000) animated:YES];
        return nil;
    }

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
        if(pinView == nil) {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
            pinView.pinColor = MKPinAnnotationColorPurple;
            pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pinView.animatesDrop = NO;
            pinView.canShowCallout = YES;
        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }

紫色图钉有一个详细信息披露按钮,但我的注释没有。如何设置这样的按钮?

这就是我可以在按下按钮时做某事的方法:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

我如何区分我的位置和所有其他人的位置,因为我需要不同的处理方式。是否有其他代表或者我必须制作某种 if 子句?

【问题讨论】:

    标签: iphone ios annotations mkmapview mkannotationview


    【解决方案1】:

    在你的didUpdateToLocation 中写类似

    AddressAnnotation   *myAnnotation = [[AddressAnnotation alloc] initWithCoordinate:currentLocation];
                    myAnnotation.title = @"You are here";
                    [self.mapView addAnnotation:myAnnotation];
    

    然后

    有点像

    - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
    {
               // try to dequeue an existing pin view first
            static NSString* annotationIdentifier = @"annotationIdentifier";
    
            NSString *titlestr = annotation.title;
    
            MKPinAnnotationView* pinView = (MKPinAnnotationView *)
            [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
            if (!pinView)
            {
                // if an existing pin view was not available, create one
               // MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                                       //initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease];
                if([titlestr isEqualToString:@"You are here"])
                {
                    customPinView.pinColor = MKPinAnnotationColorGreen;
                    NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;");
                }
                else{
                    customPinView.pinColor = MKPinAnnotationColorPurple;
                    customPinView.selected = TRUE;
                    NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;");
                    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [rightButton addTarget:self
                                    action:@selector(ShowStoreDetail:)
                          forControlEvents:UIControlEventTouchUpInside];
                    customPinView.rightCalloutAccessoryView = rightButton;
                }
                customPinView.animatesDrop = YES;
                customPinView.canShowCallout = YES;
    
                return customPinView;
            }
            else
            {
                pinView.annotation = annotation;
    
    
    
                if([titlestr isEqualToString:@"You are here"])
                {
                    customPinView.pinColor = MKPinAnnotationColorPurple;
                    NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;");
                }
                else{
                    customPinView.pinColor = MKPinAnnotationColorPurple;
                    customPinView.selected = TRUE;
                    NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;");
                    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [rightButton addTarget:self
                                    action:@selector(ShowStoreDetail:)
                          forControlEvents:UIControlEventTouchUpInside];
                    customPinView.rightCalloutAccessoryView = rightButton;
                }
    
    
            }
            return pinView;
    
        return nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2012-01-05
      • 2015-01-11
      • 1970-01-01
      • 2015-05-13
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多