由多个点构成时 MKOverlay 不显示
我有很多点(40-100),我用它们来构造一个多边形以放置在我的应用程序的地图视图上。然而,当使用这么多点时,叠加层拒绝显示,没有错误或任何东西。如果我切换回使用较少数量的点 (4-8),叠加层将再次显示。这是什么原因以及如何绕过这个问题?提前致谢!
编辑1:
这是我的地图视图中的所有相关代码:
标题:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "PermitListViewController.h"
@interface ParkingMapViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
permitTypes permitType;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic) permitTypes permitType;
-(void)loadAnnotations; //helper function that loads all annotation data from plist
-(void)loadOverlays; //helper function that loads all overlay data from plist
-(void)showCurrentLocationButtonTapped:(id)sender;
@end
实现(删除了一些与注释和标注相关的函数,因为它们在这里不相关):
#import "ParkingMapViewController.h"
#import "MKMapView+ZoomLevel.h"
#import "ParkingAnnotation.h"
#import "ParkingAnnotationView.h"
#import "PermitDetailViewController.h"
#import "ParkingRegionOverlay.h"
#import "CoreDataSingleton.h"
//#define UCD_LATITUDE <some lat>
//#define UCD_LONGITUDE <some long>
@implementation ParkingMapViewController
@synthesize mapView;
@synthesize permitType;
#pragma mark Custom Methods
- (void)showCurrentLocationButtonTapped:(id)sender {
NSLog(@"Showing current location.");
if ([mapView showsUserLocation] == NO) {
[mapView setShowsUserLocation:YES];
}
[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:13 animated:YES];
}
//...
#pragma mark Builtin Methods
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100
target:self action:@selector(showCurrentLocationButtonTapped:)];
[self loadOverlays];
[self loadAnnotations];
CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE };
[mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; //from "MKMapView+ZoomLevel.h"
}
- (parkingAnnotationType)annotationTypeLookup:(permitTypes)pType
{
if (pType == permitTypeC) {
return annotationTypeC;
} else if (pType == permitTypeL) {
return annotationTypeL;
} else if (pType == permitTypeVisitor) {
return annotationTypeVisitor;
} else if (pType == permitTypeDisabled) {
return annotationTypeDisabled;
} else if (pType == permitTypeVendor) {
return annotationTypeVendor;
} else if (pType == permitTypeBikeCommuter) {
return annotationTypeBikeCommuter;
}
return annotationTypeNone;
}
//helper function that loads all annotation data from plist
- (void)loadAnnotations{
//retrieve path of plist file and populate relevant types with its information
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PermitData" ofType:@"plist"];
NSDictionary *rootOfPermitDataPlistDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
if ([[self title] isEqualToString:@"All Permits"]) {
for (id key in rootOfPermitDataPlistDict) {
NSMutableDictionary *permitDict = [rootOfPermitDataPlistDict objectForKey:key];
//array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist)
NSArray *annotationsArray = [permitDict objectForKey:@"annotations"];
CLLocationCoordinate2D workingCoordinate;
//loop through annotations array, creating parking annotations filled with the information found in the plist
for(NSDictionary *annotationContainerDict in annotationsArray){
ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue];
workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue];
[parkingAnnot setCoordinate:workingCoordinate];
[parkingAnnot setTitle:[annotationContainerDict objectForKey:@"title"]];
[parkingAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]];
[parkingAnnot setAnnotationType:[self annotationTypeLookup:permitType]];
if (parkingAnnot.annotationType == permitTypeC) [parkingAnnot setAnnotationType:annotationTypeC];
else if (parkingAnnot.annotationType == permitTypeL) [parkingAnnot setAnnotationType:annotationTypeL];
else if (parkingAnnot.annotationType == permitTypeVisitor) [parkingAnnot setAnnotationType:annotationTypeVisitor];
else if (parkingAnnot.annotationType == permitTypeDisabled) [parkingAnnot setAnnotationType:annotationTypeDisabled];
else if (parkingAnnot.annotationType == permitTypeVendor) [parkingAnnot setAnnotationType:annotationTypeVendor];
else if (parkingAnnot.annotationType == permitTypeBikeCommuter) [parkingAnnot setAnnotationType:annotationTypeBikeCommuter];
[mapView addAnnotation:parkingAnnot];
[parkingAnnot release];
}//for
}//for
}//if
else {
NSDictionary *permitDict = [[NSDictionary alloc] initWithDictionary:[rootOfPermitDataPlistDict objectForKey:[self title]]];
//array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist)
NSArray *annotationsArray = [[NSArray alloc] initWithArray:[permitDict objectForKey:@"annotations"]];
[permitDict release];
[rootOfPermitDataPlistDict release];
CLLocationCoordinate2D workingCoordinate;
NSDictionary *annotationContainerDict = [[NSDictionary alloc] init];
//loop through annotations array, creating parking annotations filled with the information found in the plist
for(annotationContainerDict in annotationsArray){
ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue];
workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue];
[parkingAnnot setCoordinate:workingCoordinate];
[parkingAnnot setTitle:[annotationContainerDict objectForKey:@"title"]];
[parkingAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]];
if (permitType == permitTypeC) [parkingAnnot setAnnotationType:annotationTypeC];
else if (permitType == permitTypeL) [parkingAnnot setAnnotationType:annotationTypeL];
else if (permitType == permitTypeVisitor) [parkingAnnot setAnnotationType:annotationTypeVisitor];
else if (permitType == permitTypeDisabled) [parkingAnnot setAnnotationType:annotationTypeDisabled];
else if (permitType == permitTypeVendor) [parkingAnnot setAnnotationType:annotationTypeVendor];
else if (permitType == permitTypeBikeCommuter) [parkingAnnot setAnnotationType:annotationTypeBikeCommuter];
[mapView addAnnotation:parkingAnnot];
[parkingAnnot release];
}//for
[annotationContainerDict release];
[annotationsArray release];
}//else
}//loadAnnotations
//helper function that loads all overlay data from Core Data and adds it to map view
-(void)loadOverlays{
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
CoreDataSingleton *coreDataSingleton = [CoreDataSingleton sharedManager];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"ParkingLot" inManagedObjectContext:[coreDataSingleton managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [[coreDataSingleton managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSArray *pointsArray = [NSArray arrayWithArray:[info valueForKey:@"coordPoints"]];
ParkingRegionOverlay *regionPolygon = [[ParkingRegionOverlay alloc] initWithPoints:pointsArray andTitle:[info valueForKey:@"lotId"]];
[mapView addOverlay:regionPolygon];
}
[fetchRequest release];
}//loadOverlays
//...
//customizes overlay view
- (MKOverlayView *)mapView:(MKMapView *)mapView
viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[ParkingRegionOverlay class]])
{
//get the MKPolygon inside the ParkingRegionOverlay...
MKPolygon *proPolygon = ((ParkingRegionOverlay*)overlay).polygon;
MKPolygonView *aView = [[[MKPolygonView alloc]
initWithPolygon:proPolygon] autorelease];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.7];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
//[mapView setShowsUserLocation:NO];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mapView release];
[super dealloc];
}
@end
这可能是 loadOverlays 函数中的内存问题...但是所有控制台输出都应如此,但打印出更多坐标,并且应用程序永远不会因分配太多对象或其他原因而崩溃。谢谢!
I have lots of points (40-100) that I am using to construct a polygon to place on my app's map view. However when using this many points, the overlays refuse to display, there is no error or anything. If I switch back to using fewer numbers of points (4-8) the overlays display again. What is the reason for this and how can I bypass this issue? Thanks in advance!
EDIT 1:
Here is all the relevant code from my Map View:
Header:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "PermitListViewController.h"
@interface ParkingMapViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
permitTypes permitType;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic) permitTypes permitType;
-(void)loadAnnotations; //helper function that loads all annotation data from plist
-(void)loadOverlays; //helper function that loads all overlay data from plist
-(void)showCurrentLocationButtonTapped:(id)sender;
@end
Implementation (stripped out some functions pertaining to annotations and callouts, as they are not relevant here):
#import "ParkingMapViewController.h"
#import "MKMapView+ZoomLevel.h"
#import "ParkingAnnotation.h"
#import "ParkingAnnotationView.h"
#import "PermitDetailViewController.h"
#import "ParkingRegionOverlay.h"
#import "CoreDataSingleton.h"
//#define UCD_LATITUDE <some lat>
//#define UCD_LONGITUDE <some long>
@implementation ParkingMapViewController
@synthesize mapView;
@synthesize permitType;
#pragma mark Custom Methods
- (void)showCurrentLocationButtonTapped:(id)sender {
NSLog(@"Showing current location.");
if ([mapView showsUserLocation] == NO) {
[mapView setShowsUserLocation:YES];
}
[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:13 animated:YES];
}
//...
#pragma mark Builtin Methods
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100
target:self action:@selector(showCurrentLocationButtonTapped:)];
[self loadOverlays];
[self loadAnnotations];
CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE };
[mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; //from "MKMapView+ZoomLevel.h"
}
- (parkingAnnotationType)annotationTypeLookup:(permitTypes)pType
{
if (pType == permitTypeC) {
return annotationTypeC;
} else if (pType == permitTypeL) {
return annotationTypeL;
} else if (pType == permitTypeVisitor) {
return annotationTypeVisitor;
} else if (pType == permitTypeDisabled) {
return annotationTypeDisabled;
} else if (pType == permitTypeVendor) {
return annotationTypeVendor;
} else if (pType == permitTypeBikeCommuter) {
return annotationTypeBikeCommuter;
}
return annotationTypeNone;
}
//helper function that loads all annotation data from plist
- (void)loadAnnotations{
//retrieve path of plist file and populate relevant types with its information
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PermitData" ofType:@"plist"];
NSDictionary *rootOfPermitDataPlistDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
if ([[self title] isEqualToString:@"All Permits"]) {
for (id key in rootOfPermitDataPlistDict) {
NSMutableDictionary *permitDict = [rootOfPermitDataPlistDict objectForKey:key];
//array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist)
NSArray *annotationsArray = [permitDict objectForKey:@"annotations"];
CLLocationCoordinate2D workingCoordinate;
//loop through annotations array, creating parking annotations filled with the information found in the plist
for(NSDictionary *annotationContainerDict in annotationsArray){
ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue];
workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue];
[parkingAnnot setCoordinate:workingCoordinate];
[parkingAnnot setTitle:[annotationContainerDict objectForKey:@"title"]];
[parkingAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]];
[parkingAnnot setAnnotationType:[self annotationTypeLookup:permitType]];
if (parkingAnnot.annotationType == permitTypeC) [parkingAnnot setAnnotationType:annotationTypeC];
else if (parkingAnnot.annotationType == permitTypeL) [parkingAnnot setAnnotationType:annotationTypeL];
else if (parkingAnnot.annotationType == permitTypeVisitor) [parkingAnnot setAnnotationType:annotationTypeVisitor];
else if (parkingAnnot.annotationType == permitTypeDisabled) [parkingAnnot setAnnotationType:annotationTypeDisabled];
else if (parkingAnnot.annotationType == permitTypeVendor) [parkingAnnot setAnnotationType:annotationTypeVendor];
else if (parkingAnnot.annotationType == permitTypeBikeCommuter) [parkingAnnot setAnnotationType:annotationTypeBikeCommuter];
[mapView addAnnotation:parkingAnnot];
[parkingAnnot release];
}//for
}//for
}//if
else {
NSDictionary *permitDict = [[NSDictionary alloc] initWithDictionary:[rootOfPermitDataPlistDict objectForKey:[self title]]];
//array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist)
NSArray *annotationsArray = [[NSArray alloc] initWithArray:[permitDict objectForKey:@"annotations"]];
[permitDict release];
[rootOfPermitDataPlistDict release];
CLLocationCoordinate2D workingCoordinate;
NSDictionary *annotationContainerDict = [[NSDictionary alloc] init];
//loop through annotations array, creating parking annotations filled with the information found in the plist
for(annotationContainerDict in annotationsArray){
ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue];
workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue];
[parkingAnnot setCoordinate:workingCoordinate];
[parkingAnnot setTitle:[annotationContainerDict objectForKey:@"title"]];
[parkingAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]];
if (permitType == permitTypeC) [parkingAnnot setAnnotationType:annotationTypeC];
else if (permitType == permitTypeL) [parkingAnnot setAnnotationType:annotationTypeL];
else if (permitType == permitTypeVisitor) [parkingAnnot setAnnotationType:annotationTypeVisitor];
else if (permitType == permitTypeDisabled) [parkingAnnot setAnnotationType:annotationTypeDisabled];
else if (permitType == permitTypeVendor) [parkingAnnot setAnnotationType:annotationTypeVendor];
else if (permitType == permitTypeBikeCommuter) [parkingAnnot setAnnotationType:annotationTypeBikeCommuter];
[mapView addAnnotation:parkingAnnot];
[parkingAnnot release];
}//for
[annotationContainerDict release];
[annotationsArray release];
}//else
}//loadAnnotations
//helper function that loads all overlay data from Core Data and adds it to map view
-(void)loadOverlays{
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
CoreDataSingleton *coreDataSingleton = [CoreDataSingleton sharedManager];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"ParkingLot" inManagedObjectContext:[coreDataSingleton managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [[coreDataSingleton managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSArray *pointsArray = [NSArray arrayWithArray:[info valueForKey:@"coordPoints"]];
ParkingRegionOverlay *regionPolygon = [[ParkingRegionOverlay alloc] initWithPoints:pointsArray andTitle:[info valueForKey:@"lotId"]];
[mapView addOverlay:regionPolygon];
}
[fetchRequest release];
}//loadOverlays
//...
//customizes overlay view
- (MKOverlayView *)mapView:(MKMapView *)mapView
viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[ParkingRegionOverlay class]])
{
//get the MKPolygon inside the ParkingRegionOverlay...
MKPolygon *proPolygon = ((ParkingRegionOverlay*)overlay).polygon;
MKPolygonView *aView = [[[MKPolygonView alloc]
initWithPolygon:proPolygon] autorelease];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.7];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
//[mapView setShowsUserLocation:NO];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mapView release];
[super dealloc];
}
@end
It may be a memory issue within the loadOverlays function... however all the console output is just as it should be, but with more coordinates printed out, and the app never crashes due to too many objects being allocated or anything. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码一定有问题。
在写这个问题之前,我制作了一个由超过 5,000 个点组成的多边形。
你为什么不发布一些代码?
查看您的代码,我们肯定可以找出问题所在。
这是由数千个点组成的多个多边形的图像(您可以通过查看多边形的复杂性来看到这一点):
这是一个具有更多多边形的:
There must be something wrong with your code.
Right before writing this question I made a polygon consisting of over
5,000 points
.Why dont you post some of your code?
Looking at your code we can surely figure out what is going wrong.
Here is an image of several polygons consisting of thousands of points (you can see this by looking at the complexity of the polygons):
And here is one with even more polygons:
请查阅本文中标记为答案的答案:加载注释从 Plist 映射视图不起作用
Please consult the answer marked as the answer in this post: Loading Annotations to Map View From Plist Not Working