问题描述
我正在尝试在iOS 7中的地图视图上显示图像。
I'm trying to display an image over a map view in iOS 7.
我将MKOverlayRenderer子类化如下:
I subclassed MKOverlayRenderer as follows:
MapOverlayRenderer.h
MapOverlayRenderer.h
#import <Foundation/Foundation.h>
@import MapKit;
@interface MapOverlayRenderer : MKOverlayRenderer
@end
MapOverlayRenderer.m
MapOverlayRenderer.m
#import "MapOverlayRenderer.h"
@implementation MapOverlayRenderer
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
UIImage *image = [UIImage imageNamed:@"marker"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
@end
我有以下内容我的视图控制器包含地图视图:
And I have the following in my view controller which contains the map view:
ViewController.m
ViewController.m
#import "ViewController.h"
#import "MapOverlayRenderer.h"
@import MapKit;
@interface ViewController () <MKMapViewDelegate>
@property (weak,nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setDelegate:self];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MapOverlayRenderer *mapOverlay = [[MapOverlayRenderer alloc] init];
return mapOverlay;
}
- (IBAction)showSeattle:(id)sender {
CLLocationCoordinate2D location = {47.61167908,-122.33325958};
int radius = 100000; //radius in meters
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, radius, radius);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
@end
当<$ c $调用c> IBAction 将地图缩放到某个位置,我在地图视图顶部看不到图像。
When the IBAction
is called to zoom the map to a location, I do not see the image on top of the map view.
怎么能你在iOS 7中使用 MKOverlayRenderer
在地图视图上显示图像?
How can you use the MKOverlayRenderer
in iOS 7 to display images on top of a map view?
推荐答案
有几个问题:
-
通过调用
addOverlay
,代码实际上在地图中添加了一个叠加层?您需要创建一个id< MKOverlay>
类型的对象,并在addOverlay
调用中传递它。您可以创建一个实现MKOverlay
的自定义类,但在这种情况下,您可以使用MKCircle
表示图像的位置和大小。在showSeattle:
方法中:
Where is the code actually adding an overlay to the map by calling
addOverlay
? You'll need to create an object of typeid<MKOverlay>
and pass it in theaddOverlay
call. You could create a custom class that implementsMKOverlay
but in this case, you could just useMKCircle
to represent the position and size of your image. In theshowSeattle:
method:
CLLocationCoordinate2D location = {47.61167908,-122.33325958};
int radius = 100000; //radius in meters
MKCircle *c = [MKCircle circleWithCenterCoordinate:location radius:radius];
[self.mapView addOverlay:c];
MKCoordinateRegion region = ...
rendererForOverlay
,代码正在创建 MapOverlayRenderer
的实例,但没有为它提供对底层 overlay 模型对象。改为调用
initWithOverlay
:
In rendererForOverlay
, the code is creating an instance of MapOverlayRenderer
but not giving it a reference to the underlying overlay
model object. Call initWithOverlay
instead:
MapOverlayRenderer *mapOverlay
= [[MapOverlayRenderer alloc] initWithOverlay:overlay];
因此,您可以使用注释更轻松地将图像放在该位置(但是图像不会随着基于叠加层的图像的缩放级别缩放)。不确定你想要哪种功能。
By the way, you could put an image at the location much more easily using an annotation (but the image won't scale with the zoom level as an overlay-based image will). Not sure which functionality you want.
这篇关于MKOverlayRenderer在地图视图上显示UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!