Swift 如何用新颜色填充叠加层

Swift how to fill in overlay with a new color

我在填写叠加层时遇到问题。我想使用一种新颜色。我在这里先向您的帮助表示感谢。

func addBoundry()
    {
        var points=[CLLocationCoordinate2DMake(39.02, -76.9),CLLocationCoordinate2DMake(38.97, -76.9),CLLocationCoordinate2DMake(38.97, -77),CLLocationCoordinate2DMake(39.02, -77)]

        let polygon = MKPolygon(coordinates: &points, count: points.count)

        mapView.addOverlay(polygon)
    }

    let regionRadius: CLLocationDistance = 1000
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                                  regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }
    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolygon {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            //polygonView.strokeColor = UIColor.magentaColor()
            polygonView.strokeColor = UIColor(red: 0/255.0, green: 180/255.0, blue: 0/255.0, alpha: 0.4)

            return polygonView
        }

        return nil
    }

如果您想要 strokeColor 的多边形轮廓,您必须为 MKPolygonRenderer 设置 lineWidth。默认值为 0.

但是,你说"filling in the overlay"。如果您打算填写它,则应使用 fillColor,而不是 strokeColor

(当然,我还会确保您已正确指定地图视图的 delegate,并且您的 rendererForOverlay 已被调用。添加一个断点 and/or 在那里记录语句并确保例程被正确调用。)

叠加视图可以使用MKMapView的委托方法填充

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{

    MKPolylineView *overlayView = [[MKPolylineView alloc] initWithOverlay:overlay];
    overlayView.lineWidth = 5;

    if ([overlay.title isEqualToString:@"other"]){
        overlayView.strokeColor= [UIColor lightGrayColor];
        overlayView.fillColor = [UIColor lightGrayColor];
    }
    else if([overlay.title isEqualToString:@"one"]){
        overlayView.strokeColor= [UIColor blueColor];
        overlayView.fillColor = [UIColor blueColor];
    }

    return overlayView;

}