【发布时间】:2013-07-29 11:26:26
【问题描述】:
我想在地图上显示两个标志,但它们不是图钉。我为此搜索了很多,但找不到解决方案,只能添加为 pin。
请帮忙
【问题讨论】:
标签: iphone ios ipad uiimage mapkit
我想在地图上显示两个标志,但它们不是图钉。我为此搜索了很多,但找不到解决方案,只能添加为 pin。
请帮忙
【问题讨论】:
标签: iphone ios ipad uiimage mapkit
您正在寻找地图叠加层MKOverlayView。
查看这些教程:
创建叠加层
MKOverlayView
创建MKOverlayView 的子类,例如:
.h #进口 #导入
@interface MapOverlayView : MKOverlayView
{
}
@end
.m
#import "MapOverlayView.h"
@implementation MapOverlayView
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
{
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextDrawImage(ctx, theRect, imageReference);
}
@end
实现viewForOverlay:,在其中创建叠加层并添加到地图。
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MapOverlay *mapOverlay = (MapOverlay *)overlay;
MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];
return mapOverlayView;
}
【讨论】:
您可以使用MKAnnotationView 和image 属性来做到这一点,就像viewForAnnotation: 方法中的波纹管一样..
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"Current";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
if (annotation == mapView.userLocation)
return nil;
annotationView.image = [UIImage imageNamed:@"yourImageName.png"];
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
return annotationView;
}
【讨论】:
试试这个
annotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"try"];
annotation.canShowCallout = YES;
annotation.image = [UIImage imageNamed:@"image.png"];
return annotation;
并且不要使用 annotation.animatesDrop 属性。
【讨论】:
如果你在谈论MKMapView:
要显示图像而不是 Pin 注释,您需要覆盖 MKMapView 的方法
- (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation
像这样:
- (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation{
static NSString* annotationIdentifier = @"Identifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.canShowCallout = YES;
// here you need to give the image you want instead of pin
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"balloon.png"]];
return annotationView;
}
return nil;
}
【讨论】: