6

我需要在地图上显示一个 MKOverlay,但不能让它真正出现。

我正在按照Apple 的位置感知编程指南中的示例进行操作,并且不会显示叠加层。任何帮助将不胜感激,这是我制作的第一个 iPhone 应用程序,所以我可能会遗漏一些简单的东西。

导航视图控制器.h

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

@interface NavViewController : UIViewController <MKMapViewDelegate> {
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

导航视图控制器.m

#import "MSUNavViewController.h"
#import <CoreLocation/CoreLocation.h>

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];

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

        return aView;
    }
    return nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Define an overlay that covers Colorado.
    CLLocationCoordinate2D  points[4];

    points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
    points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
    points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
    points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);

    MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
    poly.title = @"Colorado";

    [_mapView addOverlay:poly];
}

故事板:

storyboard

任何编码建议将不胜感激。谢谢!

4

3 回答 3

7

确保将 mapView 设置delegate为视图控制器实例(可能File's owner在这种情况下)。

In interface builder, right-click on the map view, drag from hollowed circle at the right of delegate, to the File's Owner icon at the Placeholder section in the pane on the left.

For storyboard, connect to the View Controller icon instead of File's Owner.

于 2012-01-29T20:47:26.653 回答
0

Probably you killing aView in - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay by return nil; Try to add else before.

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];

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

        return aView;
    }
    else return nil;
} 
于 2012-01-29T20:53:23.240 回答
0

Did you solve your problem? Just drag from the circle beside delegate (in your screen shot) to the circle which is the name of the class.. there is a setting in the inspector that would allow you to display where you are also..? You made need to centre the map in say viewDidLoad so it displays over Colorado..

于 2012-08-02T19:28:02.070 回答