无法更改图钉 iOS MapKit 的颜色
Posted
技术标签:
【中文标题】无法更改图钉 iOS MapKit 的颜色【英文标题】:Trouble changing color of the pin iOS MapKit 【发布时间】:2010-10-21 19:55:15 【问题描述】:我在更改 MapKit 中注释的 pinColor 时遇到问题。当我不尝试实现 mapView:viewForAnnotation: 方法时,一切正常(添加了注释)但是当我尝试更改注释视图时,模拟器崩溃:
代码如下:
MapViewController.h
#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
@implementation MapViewController
@synthesize myMapView;
/*
// 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];
CLLocationCoordinate2D location;
location.longitude = 2.21;
location.latitude = 48.5;
MKCoordinateSpan span;
span.latitudeDelta = 1*(1 - 0);
span.longitudeDelta = 1*(1 - 0);
MKCoordinateRegion region;
region.span = span;
region.center = location;
[myMapView setRegion:region animated:NO];
[myMapView regionThatFits:region];
Annotation *someAnnotation =[[Annotation alloc] init];
[myMapView addAnnotation:someAnnotation];
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
*/
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation
MKPinAnnotationView *customPinview = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
customPinview.pinColor = MKPinAnnotationColorGreen;
customPinview.animatesDrop = YES;
customPinview.canShowCallout = YES;
return customPinview;
- (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];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[super dealloc];
@end
MapViewController.m
//
// MapViewController.h
// TestMap
//
// Created by Johan Ismael on 10/21/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController<MKMapViewDelegate>
@private
IBOutlet MKMapView *myMapView;
@property (nonatomic, retain) IBOutlet MKMapView *myMapView;
//- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation;
@end
提前致谢!!!
注意:MapViewController 在 IB 中被声明为 MapView 的委托
【问题讨论】:
【参考方案1】:在 viewForAnnotation 方法中,alloc 是在 MKAnnotationView 而不是 MKPinAnnotationView 上完成的。它一定会因“无法识别的选择器”而崩溃,因为 MKAnnotationView 没有 pinColor 属性。将分配更改为:
MKPinAnnotationView *customPinview = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
【讨论】:
谢谢!对不起这个非常愚蠢的问题......:(以上是关于无法更改图钉 iOS MapKit 的颜色的主要内容,如果未能解决你的问题,请参考以下文章