MapKit注释未显示在地图上 [英] MapKit Annotations not showing up on map

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

问题描述

我无法在MapKit的地图上显示MKAnnotationViews.我正在使用iOS 7,现在已经在论坛和网络上搜索了多个小时,尝试使用不同的示例和设置.

I'm having trouble getting the MKAnnotationViews show up on the map in MapKit. I'm using iOS 7, and have now searched the forums and the web for many hours, trying different samples and setups.

以下(我认为)我拥有最基本的设置来使其正常运行.该应用程序包含一个ViewController,顶部带有一个工具栏,顶部带有一个按钮,其余为MapView.该按钮触发方法zoomToUser:(UIBarButtonItem *)sender.右键单击并检查我的出口和代表似乎是正确的.我有一些NSLog语句被触发以输出一些调试信息.

Below I have the most basic setup possible (I think) to make it work. The app contains a single ViewController with a toolbar with a button on top, and the rest is the MapView. The button triggers the method zoomToUser:(UIBarButtonItem*)sender. Right-clicking and checking my outlets and delegates seem to be correct. I have some NSLog-statements being triggered to output some debug info.

首先是VC:

#import "ViewController.h"
#import "Data.h"

@interface ViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)zoomToUser:(UIBarButtonItem *)sender;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.showsUserLocation = YES;
    Data *ann = [[Data alloc] init];
    [self.mapView addAnnotation:ann];
}

-(void)viewDidAppear:(BOOL)animated
{
        [super viewDidAppear:animated];
        [self zoomToUser:nil];
    }

-(IBAction)zoomToUser:(UIBarButtonItem *)sender
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 2000, 2000);
    NSLog(@"region: %f x %f",self.mapView.userLocation.coordinate.latitude,self.mapView.userLocation.coordinate.longitude);
    [self.mapView setRegion:region animated:YES ];
    NSArray *arr = self.mapView.annotations;
    for(int i = 0, max = arr.count; i < max; i++)
    {
        id<MKAnnotation> annotation = arr[i];
        NSLog(@"%d of %d: %@ (%@)",i+1,max,annotation.title, [annotation class]);
    }
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    NSString *reuseId = @"Test";
    if([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    else if( [annotation isKindOfClass:[Data class]])
    {
        MKAnnotationView *annov = [mapView    dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if(!annov)
        {
            annov = [[MKAnnotationView alloc]initWithAnnotation:annotation
                                                reuseIdentifier:reuseId];
        }
        else
        {
            annov.annotation = annotation;
        }
        annov.canShowCallout = YES;
        NSLog(@"Title: %@",annotation.title);
        return annov;
    }
    else
    {
        return nil;
    }
}
@end

Data类:

#import "Data.h"

@implementation Data

-(NSString *)title
{
    return @"The title";
}

-(NSString *)subtitle
{
    return @"A subtitle";
}

-(CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 60.123456;
    coordinate.longitude = 10.123456;
    return coordinate;
}

@end

Data.h:

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

@interface Data : NSObject <MKAnnotation>

@end

还有ViewController.h:

And the ViewController.h:

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

@interface ViewController : UIViewController

@end

您可以看到viewForAnnotation方法在需要时在控制台中打印注释标题,并且每次地图平移该区域时,它都会打印.

As you can see the viewForAnnotation-method prints the annotations title in console when it's requested, and each time the map pans the area, it prints.

按下BarButton并触发zoomToUser:方法日志以控制台该地图确实具有两个注释,即MKUserLocation和我添加的一个注释.

Pressing the BarButton and triggering the zoomToUser:-method logs to console that the map indeed has two annotations, the MKUserLocation and the one I added.

为什么我的MapView告诉我它具有注释,它要求提供视图,它获得了视图,但没有显示它?

How come my MapView tells me it has the annotation, it asks for the view, it gets the view, but it does not show it?

推荐答案

viewForAnnotation中,代码正在创建并返回MKAnnotationView用于类型为Data的注释.

In viewForAnnotation, the code is creating and returning an MKAnnotationView for annotations of type Data.

默认情况下,MKAnnotationView类创建一个空视图.
也就是说,它的image属性是nil,并且视图基本上不包含任何内容.
因此,按原样,将创建和添加注释视图,但是它们是不可见的.

The MKAnnotationView class, by default, creates an empty view.
That is, its image property is nil and the view basically contains no content.
So, as-is, the annotation views are being created and added but they are invisible.

您可以:

  • MKAnnotationView上设置image属性:

annov.image = [UIImage imageNamed:@"something"];

  • 或更简单的是,创建一个MKPinAnnotationView,它是MKAnnotationView的一个方便子类,可以为您显示大头针图像:

  • Or, simpler, create an MKPinAnnotationView instead which is a convenient subclass of MKAnnotationView that displays a pin image for you:

    MKPinAnnotationView *annov = (MKPinAnnotationView *)[mapView dequeue...
    if(!annov)
    {
        annov = [[MKPinAnnotationView alloc]initWithAnnotation:annotation
                                            reuseIdentifier:reuseId];
        annov.pinColor = MKPinAnnotationColorRed;  //or Green or Purple
    }
    else
    ...
    


  • 不相关:
    您已经创建了一个自定义注释类Data,这很好,但是如果您只需要三个属性titlesubtitlecoordinate,则可以使用预定义的MKPointAnnotation类,使您可以按实例设置所有属性:


    Unrelated:
    You've created a custom annotation class Data which is fine but if all you need are the three properties title, subtitle, and coordinate, then you can just use the pre-defined MKPointAnnotation class which lets you set all the properties per-instance:

    MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
    ann.title = @"The title";
    ann.subtitle = @"A subtitle";
    ann.coordinate = CLLocationCoordinate2DMake (60.123456, 10.123456);
    [self.mapView addAnnotation:ann];
    

    这篇关于MapKit注释未显示在地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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