我正在尝试将引脚颜色从默认红色更改为自定义图像,但无论我尝试的是什么都不起作用.
我已经从这个网站下载了示例代码:
http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/
代码可以自己运行,但是当我将注释类导入到我的代码中时,它们不起作用,我不知道为什么.我也从其他网站尝试了许多不同的方法,但我甚至无法改变引脚颜色.
我的项目有4个选项卡,MapView位于其中一个选项卡上.当我选择它时,它会解析JSON字符串并将单独的注释添加到地图上.当我点击图钉时,我会显示标题和副标题,但无法更改颜色或图像.
以下是我添加注释的方法 - MapAnnotation遵循MKAnnotation:
MapAnnotation *ann = [[MapAnnotation alloc] initWithCoordinate:newCoord];
ann.title = [locationDictionary objectForKey:@"name"];
ann.subtitle = [locationDictionary objectForKey:@"name"];
[mapView addAnnotation:ann];
[ann release]
Run Code Online (Sandbox Code Playgroud)
以下是我尝试尝试更改颜色的方法 - 我在视图控制器中有MapView.delegate = self:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title]];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
}else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
Run Code Online (Sandbox Code Playgroud)
}
我得到的注释与标题和副标题一起出现,而不是绿色标记.我使用颜色或图像总是红色的.如果有人能帮助我,那就太好了!
谢谢
编辑:
mapView委托在viewDidLoad方法中指定.我还在地图的某个部分添加了叠加层.这工作正常,我也已经把它拿出来试试了它没有它导致问题,但它仍然没有解决它.
- …Run Code Online (Sandbox Code Playgroud) 我有以下文件:
annotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface annotation : NSObject <MKAnnotation>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
Run Code Online (Sandbox Code Playgroud)
annotation.m:
#import "annotation.h"
@implementation annotation
@synthesize coordinate, title, subtitle;
@end
Run Code Online (Sandbox Code Playgroud)
在主代码中,它在NSURL其他地方找到了:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];
// json parsing
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
annotation * myAnn;
NSArray *pins = mapView.annotations;
if ([pins count])
{
[mapView removeAnnotations:pins];
}
/* loop through the array, …Run Code Online (Sandbox Code Playgroud)