romaonthego / REMarkerClusterer

REMarkerClusterer creates and manages per-zoom-level clusters for large amounts of markers.
MIT License
195 stars 30 forks source link

CustomPin #37

Open kennethmatata88 opened 9 years ago

kennethmatata88 commented 9 years ago

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

markkrenek commented 8 years ago

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