3

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

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

稍后,我可能不得不在给定时间在地图上显示 20-30 个多边形。但是,在我探索如何显示叠加层(现在硬编码测试示例,而不是从文件中读取数据)时,我发现我可以显示一些叠加层,但不能显示其他叠加层。阅读 Apple 的 Location Awareness Programming Guide 时,我遇到了一个覆盖在科罗拉多州上方的多边形示例。那行得通。但是当我试图制作一个覆盖堪萨斯州的多边形时,我无法让它工作。似乎我尝试自己制作的任何多边形(安柏瑞德航空大学多边形和堪萨斯多边形)都不会显示,但我在网上获得的那些多边形工作得很好。我使用 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
4

1 回答 1

2

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

例如,这个:

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

应该

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


此外,您不应该调用您正在使用release的对象,因为它们将被自动释放。MKPolygonpolygonWithCoordinates

于 2012-08-23T02:17:23.160 回答