iPhone Mapkit将自定义图像和引脚添加到注释中 [英] iPhone Mapkit adding custom image and pins to annotations

查看:101
本文介绍了iPhone Mapkit将自定义图像和引脚添加到注释中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change the pin colour from the default red to a custom image but whatever I am trying just isn't working.

我已下载样本本网站的代码:

I have downloaded the sample code from this website:

http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/

代码可以自己运行,但是当我将注释类导入到我的代码中时,它们不起作用,我不知道为什么。我也从其他网站尝试了许多不同的方法,但我甚至无法更改引脚颜色。

The code works on it's own but when I import the annotation classes into my code, they do not work and I have no idea why. I have also tried a number of different methods from other sites but I can't even get the pin colour to change.

我的项目有4个选项卡,MapView已打开其中一个标签。当我选择它时,它会解析一个JSON字符串并将单独的注释添加到地图上。我点击图钉时会显示标题和副标题,但无法更改颜色或图像。

My project has 4 tabs and the MapView is on one of the tabs. When I select it, it parses a JSON string and adds the separate annotations onto the map. i have the title and subtitle showing up when I click on the pin, but cannot change the colour or image.

以下是我添加注释的方法 - MapAnnotation遵循MKAnnotation:

Here is how I add my annotations - MapAnnotation follows MKAnnotation:

MapAnnotation *ann = [[MapAnnotation alloc] initWithCoordinate:newCoord];
    ann.title = [locationDictionary objectForKey:@"name"];
    ann.subtitle = [locationDictionary objectForKey:@"name"];

    [mapView addAnnotation:ann];
    [ann release]

以下是我试图改变颜色的方法 - I在视图控制器中有MapView.delegate = self:

Here is how I try and attempt to change the colour - I have MapView.delegate=self in the view controller:

- (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;

}

我收到注释出现标题和副标题,而不是绿色标记。我使用颜色或图像总是红色的。如果有人能帮助我,那就太好了!

I get the annotations to appear with title and subtitle, just not green markers. They're always red wether I use colours or images. If anyone could help me out, that would be great!

谢谢

编辑:

mapView委托是在viewDidLoad方法中分配的。我还在地图的某个部分添加了叠加层。这工作正常,我也已经把它拿出去试试它没有因为它导致了一个问题,但它仍然没有解决它。

The mapView delegate is assigned in the viewDidLoad method. I also add an Overlay to a certain part of the map. This is working fine, I have also taken it out and tried it without that incase it was causing a problem but it still didn't fix it.

- (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函数是上面显示的代码但是在循环中。再次感谢您的帮助。

The doAnnotations function is the code shown above but in a loop. Thanks again for your help.

推荐答案

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

Your code looks fine to me. I suspect you have an error in your delegate assignment and mapView:viewForAnimation is not actually being called. An MKMapView without a delegate providing that function will work fine, with red pins for all annotations. Try adding some NSLog statements or setting a breakpoint in the debugger to make sure you're actually executing this code.

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

You probably already know this, but using your own images will require you to create pins that are MKAnnotationViews rather than MKAnnotationPinViews.

这篇关于iPhone Mapkit将自定义图像和引脚添加到注释中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆