且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-30 13:18:45

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;
}