如何确保在iOS MapKit中显示叠加层

时间:2012-08-23 02:10:35

标签: ios mkmapview overlay mapkit

我查看了几个关于如何在MKMapView中实现叠加的StackOverflow帖子和Apple文档。对我来说,我特别感兴趣的是在我的地图上显示MKPolygon对象。我从根本上发现,这个过程归结为以下几点:

  • 链接到MapKit和CoreLocation框架
  • 设置MKMapKit对象的出口,并将视图控制器声明为委托
  • 声明包含多边形点的CLLocationCoordinate2D数组,并使用类方法polygonWithCoordinates创建一个MKPolygon对象:count:
  • 调用map的addOverlay:并将新创建的MKPolygon对象作为参数
  • 传递
  • 实现(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay

稍后,我可能需要在地图上的给定时间显示20-30个多边形。但是,在我探索如何显示叠加层(现在硬编码测试示例,而不是从文件中读取数据)时,我发现我可以看到一些叠加层,而不是其他叠层层。阅读Apple的位置感知编程指南,我发现了一个覆盖在科罗拉多州上方的多边形的例子。那很有效。但是当我试图制作一个覆盖堪萨斯州的多边形时,我无法让它发挥作用。似乎我试图在我自己制作的任何多边形(Embry-Riddle航空大学多边形和堪萨斯多边形)都不会显示,但是那些我上网的工作完美无缺。我使用Google Earth创建多边形,然后将它们导出为KML文件以获取坐标。

我的ViewController的实现代码如下。试图找出我可能无意中做错了什么来创建这个问题。在此先感谢您的帮助。

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Array of coordinates for polygon covering state of Colorado ... DISPLAYS PERFECTLY

    CLLocationCoordinate2D points[4];

    points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
    points[1] = CLLocationCoordinate2DMake(36.99892, -109.045267);
    points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
    points[3] = CLLocationCoordinate2DMake(41.002371, -102.052066);

    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:points count:4];
    [mapView addOverlay:polygon];
    [polygon release];

    // Array of coordinates for polygon covering state of Kansas ... DOESN'T DISPLAY

    CLLocationCoordinate2D kansasPoints[9];

    kansasPoints[0] = CLLocationCoordinate2DMake(-102.0595440241806, 39.99774930940907);
    kansasPoints[1] = CLLocationCoordinate2DMake(-102.0424467175215, 36.99846609483674);
    kansasPoints[2] = CLLocationCoordinate2DMake(-94.62550551403953, 36.98936020770036);
    kansasPoints[3] = CLLocationCoordinate2DMake(-94.58798745384412, 39.11683771419185);
    kansasPoints[4] = CLLocationCoordinate2DMake(-94.79955391183, 39.21290793052091);
    kansasPoints[5] = CLLocationCoordinate2DMake(-95.13489191971419, 39.51613476830012);
    kansasPoints[6] = CLLocationCoordinate2DMake(-94.86553124171813, 39.78380472206268);
    kansasPoints[7] = CLLocationCoordinate2DMake(-95.02618283417986, 39.89072859904893);
    kansasPoints[8] = CLLocationCoordinate2DMake(-95.31904155494097, 39.99390420513669);

    MKPolygon *kansasPolygon = [MKPolygon polygonWithCoordinates:kansasPoints count:9];
    [mapView addOverlay:kansasPolygon];
    [kansasPolygon release];

    // Array of coordinates for polygon covering part of Daytona Beach, FL campus
    // of Embry-Riddle Aeronautical University... DOESN'T DISPLAY

    CLLocationCoordinate2D erauPoints[7];

    erauPoints[0] = CLLocationCoordinate2DMake(-81.05176, 29.18492);
    erauPoints[1] = CLLocationCoordinate2DMake(-81.04409, 29.18801);
    erauPoints[2] = CLLocationCoordinate2DMake(-81.05166, 29.19293);
    erauPoints[3] = CLLocationCoordinate2DMake(-81.05365, 29.19536);
    erauPoints[4] = CLLocationCoordinate2DMake(-81.05465, 29.19493);
    erauPoints[5] = CLLocationCoordinate2DMake(-81.05376, 29.19323);
    erauPoints[6] = CLLocationCoordinate2DMake(-81.05506, 29.19188);

    MKPolygon *erauPolygon = [MKPolygon polygonWithCoordinates:erauPoints count:7];
    [mapView addOverlay:erauPolygon];
    [erauPolygon release];

    // Array of coordinates taken from http://www.shawngrimes.me/2011/04/adding-polygon-map-overlays/
    // for commuter parking lot at Capitol College in Maryland ... DISPLAYS PERFECTLY

    CLLocationCoordinate2D commuterLotCoords[5]={
        CLLocationCoordinate2DMake(39.048019,-76.850535),
        CLLocationCoordinate2DMake(39.048027,-76.850234),
        CLLocationCoordinate2DMake(39.047407,-76.850181),
        CLLocationCoordinate2DMake(39.047407,-76.8505),
        CLLocationCoordinate2DMake(39.048019,-76.850535)
    };

    MKPolygon *commuterPoly1 = [MKPolygon polygonWithCoordinates:commuterLotCoords count:5];
    [mapView addOverlay:commuterPoly1];
    [commuterPoly1 release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolygon class]]) {
        MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithOverlay:overlay] autorelease];
        polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3f];
        polygonView.strokeColor = [UIColor redColor];
        polygonView.lineWidth = 1.0f;

        return polygonView;
    }

    return nil;
}

@end

1 个答案:

答案 0 :(得分:2)

看起来不显示的多边形坐标的纬度和经度参数是向后的。

例如,这个:

kansasPoints[0] = CLLocationCoordinate2DMake(-102.0595440241806, 39.99774930940907);

应该是

kansasPoints[0] = CLLocationCoordinate2DMake(39.99774930940907, -102.0595440241806);


另外,您不应该使用releaseMKPolygon创建的polygonWithCoordinates个对象上调用{{1}},因为它们会自动释放。