Swift how to fill in overlay with a new color

筅森魡賤 提交于 2020-01-17 05:48:11

问题


I am having trouble filling in the overlay. I would like to use a new color. Thanks for the help in advance.

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
    }

回答1:


If you want an outline to your polygon with strokeColor, you have to set lineWidth for your MKPolygonRenderer. The default value is 0.

But, you say "filling in the overlay". If your intent was to fill it in, you'd use fillColor, not strokeColor.

(Of course, I'd also make sure that you've specified the delegate of the map view, properly and that your rendererForOverlay is getting called at all. Add a breakpoint and/or log statement in there and make sure that routine is getting called properly.)




回答2:


The overlay view can be filled by using the delegate method of 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;

}


来源:https://stackoverflow.com/questions/38110555/swift-how-to-fill-in-overlay-with-a-new-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!