Adding subview to MKMapView that's above the map but below the annotation views?

前端 未结 3 1280
感动是毒
感动是毒 2021-02-07 06:16

For an app I\'m building, I have a custom \"old map\" effect PNG that I\'d like to overlay on the MKMapView view. Unfortunately, neither way I\'ve tried to do this is exactly ri

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 06:33

    I did something similar and in a different way in the end. I wanted to fade the mapview out by adding a white overlay, but I didn't want to fade the annotations. I added an overlay to the map (actually I had to add two as it didn't like me trying to add an overlay for the entire globe).

    CLLocationCoordinate2D pt1, pt2, pt3, pt4;
    pt1 = CLLocationCoordinate2DMake(85, 0);
    pt2 = CLLocationCoordinate2DMake(85, 180);
    pt3 = CLLocationCoordinate2DMake(-85, 180);
    pt4 = CLLocationCoordinate2DMake(-85, 0);
    CLLocationCoordinate2D coords[] = {pt1, pt2, pt3, pt4};
    MKPolygon *p = [MKPolygon polygonWithCoordinates:coords count:4];
    [self.mapView addOverlay:p];
    
    pt1 = CLLocationCoordinate2DMake(85, 0);
    pt2 = CLLocationCoordinate2DMake(85, -180);
    pt3 = CLLocationCoordinate2DMake(-85, -180);
    pt4 = CLLocationCoordinate2DMake(-85, 0);
    CLLocationCoordinate2D coords2[] = {pt1, pt2, pt3, pt4};
    MKPolygon *p2 = [MKPolygon polygonWithCoordinates:coords2 count:4];
    [self.mapView addOverlay:p2];
    

    Then you need to add the map delegate to draw them:

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {
        if ([overlay isKindOfClass:MKPolygon.class]) {
            MKPolygonRenderer *polygonView = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
            polygonView.fillColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.4];
            return polygonView;
        }
        return nil;
    }
    

提交回复
热议问题