REMarkerClusterer icon indicating copy to clipboard operation
REMarkerClusterer copied to clipboard

CustomPin

Open kennethmatata88 opened this issue 9 years ago • 1 comments

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

kennethmatata88 avatar Nov 05 '15 08:11 kennethmatata88

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

markkrenek avatar Jan 26 '16 13:01 markkrenek