1

我正在构建一个具有 MKMapview 的应用程序。我的代码所在的视图扩展了 MKMapView。

获得数据后,我创建了一个 KMLParser 并让它解析我的 KML 文件,这似乎工作得很好。我通过苹果开发人员文档中的示例获得了我的 KMLParser 的代码http://developer.apple.com/library/ios/#samplecode/KMLViewer/Introduction/Intro.html

就像我说的代码似乎工作。我的一些 KML 文件只在地图上加载了一个标记,它们工作得很好。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
4

1 回答 1

0

我发现我没有告诉视图是委托,所以底部绘制多边形层的代码没有运行。

我只是将以下内容添加到我的 initWithModel 函数中

self.delegate = self;
于 2013-02-24T13:20:33.420 回答