Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomPin #37

Open
kennethmatata88 opened this issue Nov 5, 2015 · 1 comment
Open

CustomPin #37

kennethmatata88 opened this issue Nov 5, 2015 · 1 comment

Comments

@kennethmatata88
Copy link

Dear developers!
I want to know how to make custom pin.
It doesn't work well.
Thanks.

@markkrenek
Copy link

The demo code always creates a MKPinAnnotationView, even for custom pins. It then tries to set the image property of the MKPinAnnotationView. This used to work for older OS versions, but no longer. To use a custom image, the code needs to be changed to use MKAnnotationView for custom pins. Here's my quick hack to get you pointed in the right direction. See the code bracketed inside #if 1.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(RECluster *)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *pinID;
    static NSString *defaultPinID = @"REDefaultPin";
    static NSString *clusterPinID = @"REClusterPin";
    static NSString *markerPinID = @"REMarkerPin";

    if (self.segmentedControl.selectedSegmentIndex == 0) {
        pinID = defaultPinID;
    } else {
        pinID = annotation.markers.count == 1 ? markerPinID : clusterPinID;
    }

#if 1 // MAK

    if (self.segmentedControl.selectedSegmentIndex == 1)    // custom pins
    {
        MKAnnotationView *pinView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (!pinView)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
            UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            detailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            detailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            pinView.rightCalloutAccessoryView = detailButton;
            pinView.canShowCallout = YES;
        }
        pinView.image = [UIImage imageNamed:annotation.markers.count == 1 ? @"Pin_Red" : @"Pin_Purple"];

        return pinView;
    }

#endif

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinID];

    if (pinView == nil) {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        detailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        detailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        pinView.rightCalloutAccessoryView = detailButton;
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;

        if (self.segmentedControl.selectedSegmentIndex == 1) {
            pinView.image = [UIImage imageNamed:annotation.markers.count == 1 ? @"Pin_Red" : @"Pin_Purple"];
        }
    }

    return pinView;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants