我想在地图上显示一些注释。我想使用MKMapView
类,因为它处理注释的方式,这对我来说很好。但是我的自定义地图系统可以使用自己的视图。我试图按照这个答案的建议实现方法调配:https://stackoverflow.com/a/10702022/1152596,但我没有运气。瓷砖没有显示,这是好的,但背景不透明,它有一个类似灰色的颜色。见下面的截图:
黑色路径是我的注释。后面是我自己的地图我看不到的视图。我确定它落后了,如果我不添加MKMapView
我可以看到它。
我知道我在这里尝试的是非常hacky,但我无法想到。
链接答案的混合代码是:
我定义:
// Import runtime.h to unleash the power of objective C
#import <objc/runtime.h>
// this will hold the old drawLayer:inContext: implementation
static void (*_origDrawLayerInContext)(id, SEL, CALayer*, CGContextRef);
// this will override the drawLayer:inContext: method
static void OverrideDrawLayerInContext(UIView *self, SEL _cmd, CALayer *layer, CGContextRef context)
{
// uncommenting this next line will still perform the old behavior
//_origDrawLayerInContext(self, _cmd, layer, context);
// change colors if needed so that you don't have a black background
layer.backgroundColor = RGB(35, 160, 211).CGColor;
CGContextSetRGBFillColor(context, 35/255.0f, 160/255.0f, 211/255.0f, 1.0f);
CGContextFillRect(context, layer.bounds);
}
在我的viewDidLoad
方法中:
UIView* scrollview = [[[[mapView subviews] objectAtIndex:0] subviews] objectAtIndex:0];
UIView* mkTiles = [[scrollview subviews] objectAtIndex:0]; // <- MKMapTileView instance
// Retrieve original method object
Method origMethod = class_getInstanceMethod([mkTiles class],
@selector(drawLayer:inContext:));
// from this method, retrieve its implementation (actual work done)
_origDrawLayerInContext = (void *)method_getImplementation(origMethod);
// override this method with the one you created
if(!class_addMethod([mkTiles class],
@selector(drawLayer:inContext:),
(IMP)OverrideDrawLayerInContext,
method_getTypeEncoding(origMethod)))
{
method_setImplementation(origMethod, (IMP)OverrideDrawLayerInContext);
}