我正在尝试将图钉颜色从默认红色更改为自定义图像,但我所做的任何尝试都不起作用。
我从这个网站下载了示例代码:
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]
这是我尝试更改颜色的方法 - 我在视图控制器中有 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;
}
我得到的注释与标题和副标题一起出现,但不是绿色标记。无论我使用颜色还是图像,它们总是红色的。如果有人能帮助我,那就太好了!
Thanks
EDIT:
mapView 委托在 viewDidLoad 方法中分配。我还在地图的某个部分添加了叠加层。这工作正常,我也把它拿出来尝试了一下,以防万一它引起了问题,但它仍然没有解决它。
- (void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion cordRgn;
LusuAppAppDelegate *delegate =[[UIApplication sharedApplication]delegate];
if (delegate.CurrentLocation == 0) {
cordRgn.center.latitude = (CENTRE_OF_POSITION_2_LAT);
cordRgn.center.longitude = (CENTRE_OF_POSITION_2_LON);
delegate.CurrentLocation = 1;
}else if (delegate.CurrentLocation == 1) {
cordRgn.center.latitude = (CENTRE_OF_POSITION_1_LAT);
cordRgn.center.longitude = (CENTRE_OF_POSITION_1_LON);
delegate.CurrentLocation = 0;
}
cordRgn.span.latitudeDelta = 0.009f;
cordRgn.span.longitudeDelta = 0.009f;
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
//add overlay
MKRasterOverlay *overlay = [[MKRasterOverlay alloc] init];
[overlay setCoordinate:CLLocationCoordinate2DMake(54.005508, -2.780507)];
MKMapRect mkrect;
MKMapPoint mkpointa, mkpointb;
mkrect.origin = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.016964, -2.794862));
mkpointa = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.016964, 2.000000));
mkpointb = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.001447, 2.013770));
mkrect.size.width = mkpointb.x - mkpointa.x;
mkrect.size.height = mkpointb.y - mkpointa.y;
overlay.boundingMapRect = mkrect;
mapView.delegate = self;
[mapView addOverlay:overlay];
[mapView setRegion:cordRgn animated:NO];
[self.mapView setShowsUserLocation:YES];
[self doAnnotations];
}
doAnnotations 函数是上面显示的代码,但处于循环中。再次感谢你的帮助。