我正在构建一个具有MKMapview的应用程序。我的代码所在的视图扩展了MKMapView。
获取数据后,我创建了一个KMLParser并让它解析我的KML文件似乎工作正常。我通过这里的开发人员文档http://developer.apple.com/library/ios/#samplecode/KMLViewer/Introduction/Intro.html
中的示例获取了KMLParser的代码 像我说的那样代码似乎有效。我的一些KML文件只将Marker加载到地图上,它们工作得很好。这是需要绘制多边形叠加层的KML文件,这些叠加层看起来似乎没有渲染。我想知道我底部附近的MKMapViewDelegate代码是不是做了它需要做的事情?我把破发点放在那里,他们似乎永远不会受到打击。以下是我的观点.h和.m的样子。
.h
#import <MapKit/MapKit.h>
#import "MainDM.h"
#import "BBView.h"
#import "MapPM.h"
#import "KMLParser.h"
@class MapPM;
@class MainDM;
@interface MapView : MKMapView <BBView, MKMapViewDelegate>
{
NSArray *overlays;
KMLParser *parser;
}
@property(nonatomic,strong)MainDM* dm;
@property(nonatomic,strong)MapPM* pm;
-(void)update:(DataObject*)data;
-(id)initWithModel:(MainDM*)dm:(CGRect)frame;
@end
的.m
#import "MapView.h"
@implementation MapView
-(id)initWithModel:(MainDM*)dm:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_dm = dm;
[_dm register:self];
_pm = [[MapPM alloc] initWithModel:_dm];
CLLocationCoordinate2D coord = {.latitude = 32.61724, .longitude = -106.74128};
MKCoordinateSpan span = {.latitudeDelta = 1, .longitudeDelta = 1};
MKCoordinateRegion region = {coord, span};
[self setRegion:region];
}
return self;
}
-(void)update:(DataObject*)data
{
if([[data getType] isEqualToString:@"CURRENT_KML_CHANGE"])
{
KmlVO *d = (KmlVO*)[data getData];
parser = [_pm showMKL:d];
// Add all of the MKOverlay objects parsed from the KML file to the map.
NSArray *o = [parser overlays];
[self addOverlays:o];
// Add all of the MKAnnotation objects parsed from the KML file to the map.
NSArray *annotations = [parser points];
[self addAnnotations:annotations];
// Walk the list of overlays and annotations and create a MKMapRect that
// bounds all of them and store it into flyTo.
MKMapRect flyTo = MKMapRectNull;
for (id <MKOverlay> overlay in o)
{
if (MKMapRectIsNull(flyTo))
{
flyTo = [overlay boundingMapRect];
}
else
{
flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
}
}
for (id <MKAnnotation> annotation in annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo))
{
flyTo = pointRect;
}
else
{
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
// Position the map so that all overlays and annotations are visible on screen.
//self.visibleMapRect = flyTo;
}
}
#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
return [parser viewForOverlay:overlay];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
return [parser viewForAnnotation:annotation];
}
@end
答案 0 :(得分:0)
我发现我没有告诉视图是委托,因此底部绘制多边形图层的代码没有运行。
我只是将以下内容添加到我的initWithModel函数
中self.delegate = self;