如何:在MKMapView上显示MKOverlay [英] How to: Display MKOverlay on MKMapView

查看:177
本文介绍了如何:在MKMapView上显示MKOverlay的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图上显示MKOverlay,但无法让它实际显示。

I need to display an MKOverlay on a map, but can't get it to actually appear.

我正在关注示例 .apple.com / library / ios / #documentation / UserExperience / Conceptual / LocationAwarenessPG / AnnotatingMaps / AnnotatingMaps.htmlrel =noreferrer>位置感知编程指南和覆盖图不会显示。任何帮助将不胜感激,这是我做的第一个iPhone应用程序,所以我可能会遗漏一些简单的东西。

I'm following the example from Apple's Location Awareness Programming Guide and the overlay wont display. Any help would be greatly appreciated, this is the first iPhone app I've made, so I might be missing something simple.

NavViewController.h

NavViewController.h

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

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

@end

NavViewController.m

NavViewController.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];
}

故事板:

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

Any coding suggestions would be greatly appreciated. Thanks!

推荐答案

确保设置mapView的委托到视图控制器实例(在这种情况下可能文件的所有者)。

Make sure to set mapView's delegate to the view controller instance( perhaps File's owner in this case).

在界面构建器中,右键单击地图视图,从委托右侧的空心圆圈拖动到左侧窗格中占位符部分的文件所有者图标。

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.

对于故事板,请连接到View Controller图标而不是File的所有者。

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

这篇关于如何:在MKMapView上显示MKOverlay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆