наложение карты не отображается - PullRequest
0 голосов
/ 18 января 2019

Я пытаюсь сделать MKMapView и добавить и наложить на него, используя координаты. Он не отображается в mapView.

кажется, что он компилируется нормально, но когда я открываю представление mapView в раскадровке, я вижу карту мира, но нигде нет красного наложения.

Вот мой код.

мой заголовок

#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

и мой .m файл

#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

Что я пытаюсь сделать, так это нарисовать наложение для штата Аризона. Я проверил координаты, и они верны.

В конце концов я хочу создать оверлеи для всех состояний.

Кто-нибудь может увидеть, где я иду не так?

1 Ответ

0 голосов
/ 19 января 2019

Я понял это, изменив свой подход. Вот мой код, который работает. Сначала я создаю массив моих координат CLLocationCoordinate2D и превращаю их в многоугольник. затем добавьте в 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];

затем я отрисовываю это в виде карты.

- (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;
}

Затем я проверяю, находится ли мое местоположение внутри многоугольника.

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?????????????????????????????????");

// сделать что-то еще } * +1010 *

            CGPathRelease(mpr);
        }
    }

Сначала я не хотел добавлять вид карты, но потом решил, что мне нужно, и теперь, когда он у меня есть, он мне нравится.

...