gpt4 book ai didi

ios - 如何确保 iOS MapKit 中叠加层的显示

转载 作者:行者123 更新时间:2023-12-02 03:01:46 25 4
gpt4 key购买 nike

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

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

稍后,我可能需要在给定时间在 map 上显示 20-30 个多边形。然而,在我探索如何显示叠加层(现在对测试示例进行硬编码,而不是从文件中读取数据)时,我发现我可以显示一些叠加层,但不能显示其他叠加层。在阅读苹果公司的位置感知编程指南时,我发现了一个覆盖在科罗拉多州上方的多边形示例。那行得通。但是当我尝试制作一个覆盖堪萨斯州的多边形时,我无法让它工作。似乎我尝试自己制作的任何多边形(安柏瑞德航空大学多边形和堪萨斯多边形)都不会显示,但我在网上获得的多边形工作得很好。我使用 Google 地球创建多边形,然后将它们导出为 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

最佳答案

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

例如:

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

应该是

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


另外,您不应该对使用 polygonWithCooperatives 创建的 MKPolygon 对象调用 release,因为它们会自动释放。

关于ios - 如何确保 iOS MapKit 中叠加层的显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com