iphone - MKMapView -> 为我的位置显示一个按钮

标签 iphone ios annotations mkmapview mkannotationview

我有一个使用这些源代码行实现的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 子句?

最佳答案

在你的 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;
}

关于iphone - MKMapView -> 为我的位置显示一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2066362/

相关文章:

iphone - 如何将 ViewController 添加到 UIPageViewController

ios - 如何将 Unity 集成到 native iOS 应用程序中?

ios - XCode 6.3 Beta 将 iOS 8.3b 设备显示为 "ineligible"

c# - 警告 CS0114 : `AnApp.AppDelegate.Self' hides inherited member

java - getAnnotations() 为空并且未按预期工作

python - collections.Iterable vs typing.Iterable 在类型注释和检查 Iterable

java - 关于Customer Annotation Processor,为什么getElementsAnnotatedWith()返回的set大小为0

iphone - 引用 AppDelegate 方法 - iPhone

android - 对于想要使用 .NET 的移动开发人员来说,Mono 是一个不错的选择吗?

ios - 无法正确显示核心数据中的图像