将MKPolygon作为叠加层添加到MKMapView时出现问题

时间:2010-12-03 09:40:09

标签: iphone ios mkmapview overlay

Ey那里,正如标题所说,我很难将MKPolygon添加为MKMapView的叠加层。以下是相关代码:
ParkingMapViewContoller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>


@interface ParkingMapViewController : UIViewController <MKMapViewDelegate> {
    MKMapView *mapView;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;


-(void)loadAnnotations;
-(void)showCurrentLocationButtonTapped:(id)sender;


@end

ParkingMapViewController.m

//...
#import "ParkingRegionOverlay.h"
//...
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.title);

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)];

    /*MKMapPoint points[3] = {{38.53607,-121.765793}, {38.537606,-121.768379}, {38.53487,-121.770578}};
    MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:3];*/

    ParkingRegionOverlay *polygon = [[ParkingRegionOverlay alloc] initialize];
    [mapView addOverlay:polygon];

    [self loadAnnotations];

    CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE };
    [mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; //from "MKMapView+ZoomLevel.h"
}
//...

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    NSLog(@"in viewForOverlay!");

    if ([overlay isKindOfClass:[MKPolygon class]])

    {

        MKPolygonView*    aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease];

        aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];

        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

        aView.lineWidth = 3;

        return aView;

    }
    return nil;
}
//...

ParkingRegionOverlay.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface ParkingRegionOverlay : NSObject <MKOverlay> {
    //CLLocationCoordinate2D origin;
    MKPolygon *polygon;

    //MKMapRect rect;
}

//@property (nonatomic) CLLocationCoordinate2D origin;
@property (nonatomic, retain) MKPolygon *polygon;

//@property (nonatomic) MKMapRect rect;

-(ParkingRegionOverlay*)initialize;
-(MKMapRect)boundingMapRect;
-(CLLocationCoordinate2D)coordinate;

@end

ParkingRegionOverlay.m

#import "ParkingRegionOverlay.h"


@implementation ParkingRegionOverlay

//@synthesize origin;
@synthesize polygon;

//@synthesize rect;


-(ParkingRegionOverlay*) initialize {
    MKMapPoint points[3] = {{38.53607,-121.765793}, {38.537606,-121.768379}, {38.53487,-121.770578}};
    polygon = [MKPolygon polygonWithPoints:points count:3];
    polygon.title = @"Some Polygon";
    return self;
}

- (MKMapRect)boundingMapRect{
    MKMapRect bounds = MKMapRectMake(-121.770578,38.537606,-121.770578-(-121.765793),38.537606-38.53487);
    return bounds;
}

- (CLLocationCoordinate2D)coordinate{
    return CLLocationCoordinate2DMake((38.537606-38.53487)/2, (-121.770578-(-121.765793))/2);
}

@end

你看到NSLog我陷入了viewForOverlay:方法吗?好吧,它永远不会出现在控制台中,因此永远不会调用该函数。什么是错的?非常感谢!

3 个答案:

答案 0 :(得分:10)

主要问题是代码在地图视图中给出了期望MKMapPoints的纬度/经度坐标。有关差异的说明,请参阅Location Awareness Programming Guide中的“了解地图几何”。使用MKMapPointForCoordinate函数将纬度/经度坐标转换为MKMapPoint。

第二个问题是在viewForOverlay中,它正在检查overlay是否为MKPolygon类型。您的overlay类ParkingRegionOverlay包含一个MKPolygon对象,但它本身不是MKPolygon类型。

要解决主要问题,您需要更改initialize和boundingMapRect方法:

-(id)init {
    if (self = [super init]) {
        MKMapPoint points[3];
        CLLocationCoordinate2D c1 = {38.53607,-121.765793};
        points[0] = MKMapPointForCoordinate(c1);
        CLLocationCoordinate2D c2 = {38.537606,-121.768379};
        points[1] = MKMapPointForCoordinate(c2);
        CLLocationCoordinate2D c3 = {38.53487,-121.770578};
        points[2] = MKMapPointForCoordinate(c3);

        polygon = [MKPolygon polygonWithPoints:points count:3];
        polygon.title = @"Some Polygon";
    }
    return self;
}

- (MKMapRect)boundingMapRect{
    CLLocationCoordinate2D corner1 = 
        CLLocationCoordinate2DMake(38.537606, -121.770578);
    MKMapPoint mp1 = MKMapPointForCoordinate(corner1);

    CLLocationCoordinate2D corner2 = 
        CLLocationCoordinate2DMake(38.53487, -121.765793);
    MKMapPoint mp2 = MKMapPointForCoordinate(corner2);

    MKMapRect bounds = 
        MKMapRectMake(mp1.x, mp1.y, (mp2.x-mp1.x), (mp2.y-mp1.y));

    return bounds;
}

请注意我将方法“initialize”更改为“init”的方式。虽然它没有阻止多边形显示,但是使用名为“initialize”且不调用[super init]的方法覆盖ParkingRegionOverlay初始化的方式不符合约定。 (还要从.h文件中删除“初始化”。)

要修复第二个问题,viewForOverlay方法应如下所示:

- (MKOverlayView *)mapView:(MKMapView *)mapView 
    viewForOverlay:(id <MKOverlay>)overlay
{
    NSLog(@"in viewForOverlay!");

    if ([overlay isKindOfClass:[ParkingRegionOverlay class]])
                              //^^^^^^^^^^^^^^^^^^^^
    {
        //get the MKPolygon inside the ParkingRegionOverlay...
        MKPolygon *proPolygon = ((ParkingRegionOverlay*)overlay).polygon;

        MKPolygonView *aView = [[[MKPolygonView alloc] 
            initWithPolygon:proPolygon] autorelease];
                          //^^^^^^^^^^

        aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        aView.lineWidth = 3;

        return aView;
    }
    return nil;
}

最后,更改viewDidLoad中的代码:

ParkingRegionOverlay *polygon = [[ParkingRegionOverlay alloc] init];
[mapView addOverlay:polygon];
[polygon release]; //don't forget this

答案 1 :(得分:0)

在我看来,你忘了设置你的代表 类似的东西

_ mapView.delegate = self;

答案 2 :(得分:0)

你设置了吗?:

yourMap.delegate=self;

你的目标是什么? 3.x或4.x ??我认为MKPolygon可以从4.0获得。 您可以尝试将import子句添加到前缀文件中,如:

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#endif