问题
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