Question

I'm doing a project utilizing the Mapkit framework. Initially there is a map of Florida with many annotations and circular overlays that show which regions receive delivery. I'd like to have all of those, as well as a MKPolygon to highlight the entire state. (which is done by using an XML file of all the coordinates and the XMLParser in Objective-C)

The MKCircleView overlays all display fine which I'm guessing happens because they are all of the same type. However, when I try to implement the MKPolygon view also, it does not allow me to.

//Region for fort lauderdale delivery area overlay

MKCoordinateRegion fortLauderdale;
fortLauderdale.center.latitude = FTL_LATITUDE;
fortLauderdale.center.longitude = FTL_LONGITUDE;
fortLauderdale.span.latitudeDelta = SPAN_VALUE;
fortLauderdale.span.longitudeDelta = SPAN_VALUE;

//Create a circle for fort lauderdale area
MKCircle *fortLauderdaleCircle= [MKCircle circleWithCenterCoordinate:fortLauderdale.center radius:5000];
//2KM or 1.24 miles
[self.mapView addOverlay:fortLauderdaleCircle];

//Region for miami delivery area overlay

MKCoordinateRegion miami;
miami.center.latitude = MIA_LATITUDE;
miami.center.longitude = MIA_LONGITUDE;
miami.span.latitudeDelta = SPAN_VALUE;
miami.span.longitudeDelta = SPAN_VALUE;

//Create a circle for miami area
MKCircle *miamiCircle = [MKCircle circleWithCenterCoordinate:miami.center radius:5000];
//2KM or 1.24 miles
[self.mapView addOverlay:miamiCircle];

Then in the method to implement them I have this:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    MKCircleView *circleView = [[MKCircleView alloc]initWithCircle:overlay];
    circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
    circleView.strokeColor =[[UIColor blueColor] colorWithAlphaComponent:0.6];
    circleView.lineWidth = 1 ;
    return circleView;

    //This is the code for the polygon view but I don't know how
    //to return it without run-time errors 
    //MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon:overlay];
    //polygonView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.2];
    //polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.6];
    //polygonView.lineWidth = 1;
    //return polygonView;
}

This already looks bad because I'm calling two return statements in the method, but then again thats why I'm asking this question. I don't think that the error is in the way I parsed the XML data because before the implementation of it there were no errors. Thank you

Was it helpful?

Solution

First of all, I would like to point out that you are using deprecated methods. MKOverlayView as deprecated in iOS7. That aside though, what you need to do is determine which type of overlay to return based on the overlay parameter passed to the delegate. So, if I were to modify your code, this is what I would do:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleView *circleView = [[MKCircleView alloc]initWithCircle:overlay];
        circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
        circleView.strokeColor =[[UIColor blueColor] colorWithAlphaComponent:0.6];
        circleView.lineWidth = 1 ;
        return circleView;
    } else if ([overlay isKindOfClass:[MKPolygon class]]) {
        MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon:overlay];
        polygonView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.2];
        polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.6];
        polygonView.lineWidth = 1;
        return polygonView;
    }

    return nil;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top