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

发布于 2024-10-10 08:10:33 字数 3240 浏览 1 评论 0原文

我正在尝试将图钉颜色从默认红色更改为自定义图像,但我所做的任何尝试都不起作用。

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

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;

}

我得到的注释与标题和副标题一起出现,只是没有绿色标记。无论我使用颜色还是图像,它们总是红色的。如果有人能帮助我,那就太好了!

谢谢

编辑:

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

函数是上面显示的代码,但处于循环中。再次感谢您的帮助。

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.

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.

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]

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!

Thanks

EDIT:

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

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-10-17 08:10:33

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文