iPhone Mapkit将自定义图像和引脚添加到注释中

omn*_*ian 3 iphone objective-c mapkit

我正在尝试将引脚颜色从默认红色更改为自定义图像,但无论我尝试的是什么都不起作用.

我已经从这个网站下载了示例代码:

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方法中指定.我还在地图的某个部分添加了叠加层.这工作正常,我也已经把它拿出来试试了它没有它导致问题,但它仍然没有解决它.

- (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];
Run Code Online (Sandbox Code Playgroud)

}

doAnnotations函数是上面显示的代码,但是在循环中.再次感谢你的帮助.

Sea*_*ell 7

你的代码看起来很好.我怀疑你的委托任务有错误,mapView:viewForAnimation实际上并没有被调用.没有提供该功能的委托的MKMapView将正常工作,所有注释都带有红色引脚.尝试NSLog在调试器中添加一些语句或设置断点,以确保您实际执行此代码.

您可能已经知道这一点,但使用您自己的图像将需要您创建MKAnnotationViews而不是MKAnnotationPinViews的引脚.