覆盖地图视图未显示
overlay map view not showing up
我正在尝试制作 MKMapView 并使用坐标添加和覆盖它。
它没有显示在 mapView 中。
它似乎编译正常,但是当我在故事板中打开 mapView 视图时,我看到了世界地图,但任何地方都没有红色覆盖。
这是我的代码。
我的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
和我的 .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
我想要做的是为亚利桑那州绘制一个叠加层。我已经检查了坐标,它们是正确的。
最终我想为所有州创建叠加层。
谁能看出我错在哪里?
我通过改变我的方法弄明白了。这是我正在运行的代码。
首先,我创建一个坐标的 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?????????????????????????????????");
//做点别的
}
CGPathRelease(mpr);
}
}
起初我不想添加地图视图,但后来决定我需要添加,现在我喜欢它。
我正在尝试制作 MKMapView 并使用坐标添加和覆盖它。 它没有显示在 mapView 中。
它似乎编译正常,但是当我在故事板中打开 mapView 视图时,我看到了世界地图,但任何地方都没有红色覆盖。
这是我的代码。
我的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
和我的 .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
我想要做的是为亚利桑那州绘制一个叠加层。我已经检查了坐标,它们是正确的。
最终我想为所有州创建叠加层。
谁能看出我错在哪里?
我通过改变我的方法弄明白了。这是我正在运行的代码。 首先,我创建一个坐标的 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?????????????????????????????????");
//做点别的 }
CGPathRelease(mpr);
}
}
起初我不想添加地图视图,但后来决定我需要添加,现在我喜欢它。