如何确保iOS MapKit中覆盖层的显示

3

我查看了几篇关于在MKMapView中实现覆盖物的StackOverflow帖子和苹果文档。对我而言,我特别感兴趣的是在地图上显示MKPolygon对象。基本上,这个过程可以归结为以下几步:

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

稍后,我可能需要在地图上同时显示20-30个多边形。然而,在探索如何显示覆盖物时(现在正在硬编码测试示例,而不是从文件中读取数据),我发现我可以让一些覆盖物出现,但其他覆盖物却不能。阅读苹果的“位置感知编程指南”时,我遇到了一个在科罗拉多州上方叠加的多边形的示例。那可以工作。但是当我尝试制作覆盖堪萨斯州的多边形时,我无法让它工作。似乎我自己尝试制作的任何多边形(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个回答

2

看起来没有显示的多边形的经纬度坐标参数是反过来的。

例如,这样:

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

应该是

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

此外,您不应该在使用polygonWithCoordinates创建的MKPolygon对象上调用release方法,因为它们将被自动释放。

好吧,那是一个愚蠢的错误。 :) 我想我已经看了这么长时间,以至于我甚至没有注意到参数倒置。感谢您的帮助。像魔法一样奏效! - user1195536

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接