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

时间:2016-06-29 21:44:03

标签: ios swift mapkit overlay fill

我无法填写叠加层。我想用一种新颜色。我在这里先向您的帮助表示感谢。

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
    }

2 个答案:

答案 0 :(得分:3)

如果您希望使用strokeColor为多边形绘制轮廓,则必须为lineWidth设置MKPolygonRenderer。默认值为0

但是,你说“填写叠加层”。如果您的意图是填写,请使用fillColor,而不是strokeColor

(当然,我还要确保您已正确指定了地图视图的delegate并且根本调用了rendererForOverlay。添加断点和/或在那里记录日志语句并确保正常调用例程。)

答案 1 :(得分:0)

可以使用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;

}