Search code examples
iosobjective-cmapkitmkoverlay

overlay map view not showing up


I'm trying to make a MKMapView and add and overlay to it using coordinates. It is not showing up in the mapView.

it seems to be compiling ok but when I open the mapView view in the storyboard I see the map of the world but there is no red overlay anywhere.

Here is my code.

my header

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreMotion/CoreMotion.h>
#import <MapKit/MapKit.h>
NS_ASSUME_NONNULL_BEGIN

@interface mapViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@property(nonatomic, retain) MKPolygon *polygon;

@end

and my .m file

#import "mapViewController.h"

@interface mapViewController ()

@end

@implementation mapViewController
@synthesize mapView,locationManager,polygon;
- (void)viewDidLoad {
    [super viewDidLoad];
    mapView.delegate = self;
    NSDictionary *d1=[NSDictionary dictionaryWithObjectsAndKeys:@"37.57111111",@"x",@"-109.52166667",@"y",nil];
    NSDictionary *d2=[NSDictionary dictionaryWithObjectsAndKeys:@"32.01138889",@"x",@"-109.59000000",@"y",nil];
    NSDictionary *d3=[NSDictionary dictionaryWithObjectsAndKeys:@"31.83166667",@"x",@"-111.95416667",@"y",nil];
    NSDictionary *d4=[NSDictionary dictionaryWithObjectsAndKeys:@"32.34250000",@"x",@"-115.14333333",@"y",nil];
    NSDictionary *d5=[NSDictionary dictionaryWithObjectsAndKeys:@"34.89722222",@"x",@"-115.47611111",@"y",nil];
    NSDictionary *d6=[NSDictionary dictionaryWithObjectsAndKeys:@"37.32083333",@"x",@"-116.02027778",@"y",nil];
    NSDictionary *d7=[NSDictionary dictionaryWithObjectsAndKeys:@"37.53305556",@"x",@"-114.73416667",@"y",nil];
    NSDictionary *d8=[NSDictionary dictionaryWithObjectsAndKeys:@"37.53166667",@"x",@"-114.11277778",@"y",nil];
    NSDictionary *d9=[NSDictionary dictionaryWithObjectsAndKeys:@"37.57111111",@"x",@"-109.52166667",@"y",nil];


    NSArray *azcoord = [NSArray arrayWithObjects:d1,d2,d3,d4,d5,d6,d7,d8, nil];
    CLLocationCoordinate2D coordinates[azcoord.count];

    int coordinatesIndex = 0;

    for (NSDictionary * c in azcoord) {
        double x = [[c valueForKey:@"x"] doubleValue];
        double y = [[c valueForKey:@"y"] doubleValue];

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = y;
        coordinate.longitude = x;

        //Put this coordinate in the C array...
        coordinates[coordinatesIndex] = coordinate;

        coordinatesIndex++;
    }

    polygon = [MKPolygon polygonWithCoordinates:coordinates count:azcoord.count];


    [self.mapView addOverlay:polygon];


}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];
    renderer.fillColor   = [[UIColor redColor] colorWithAlphaComponent:1];
    renderer.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    renderer.lineWidth   = 2;
    return renderer;
}



@end

what I am trying to do is draw an overlay for the state of Arizona. I've checked the coordinates and they are correct.

Eventually I want to create overlays for all states.

can anyone see where I'm going wrong?


Solution

  • I figured it out by changing my approach. Here is my code that is working. first i create a CLLocationCoordinate2D array of my coordinates and make them into a polygon. then add to mapView.

    CLLocationCoordinate2D points[7];
        points [0] = CLLocationCoordinate2DMake(36.99910000, -109.04520000);
        points [1] = CLLocationCoordinate2DMake(31.33460000, -109.05220000);
        points [2] = CLLocationCoordinate2DMake(31.33700000, -111.07720000);
        points [3] = CLLocationCoordinate2DMake(32.49410000, -114.81360000);
        points [4] = CLLocationCoordinate2DMake(36.10660000, -114.75180000);
        points [5] = CLLocationCoordinate2DMake(36.21600000, -114.03940000);
        points [6] = CLLocationCoordinate2DMake(37.00150000, -114.04820000);
    
    
        polygon = [MKPolygon polygonWithCoordinates:points count:7];
    
    
       [self.mapView addOverlay:polygon];
    

    then I render that to the map view.

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
    { NSLog(@"YES!!!!!!!!!!!!!!!!!");
        MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];
        renderer.fillColor   = [[UIColor redColor] colorWithAlphaComponent:0.2];
        renderer.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 1;
        return renderer;
    }
    

    then I check to see if my location is inside the polygon.

    CLLocationCoordinate2D sampleLocation = CLLocationCoordinate2DMake(myLatitude,myLongitude);//13,80 is the latlong of clear colored area of the MKPolygon in the below image.
        MKMapPoint mapPoint = MKMapPointForCoordinate(sampleLocation);
        CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
    
        for (id<MKOverlay> polygon in mapView.overlays) {
            if([polygon isKindOfClass:[MKPolygon class]]){
                NSLog(@"YES<<<<<<<<<<<<<<<<<<");
                MKPolygon *polygons = (MKPolygon*) polygon;
    
                CGMutablePathRef mpr = CGPathCreateMutable();
    
                MKMapPoint *polygonPoints = polygons.points;
    
                for (int p=0; p < polygons.pointCount; p++){
                    MKMapPoint mp = polygonPoints[p];
                    if (p == 0)
                        CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
                    else
                        CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
                }
    
                if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, TRUE)){
                    NSLog(@"YES?????????????????????????????????");
    

    //do something else }

                CGPathRelease(mpr);
            }
        }
    

    I at first didn't want to add a map view but then decided I need to and now that I have I like it.