简体   繁体   English

mapView:didAddAnnotationViews:不响应

[英]mapView:didAddAnnotationViews: doesn't respond

I am currently reading the iPhone Programming The Big Nerd Ranch Guide and encounter some problem on page 96. 我目前正在阅读《 iPhone编程大书呆子牧场指南》,并在第96页遇到问题。

I have assigned the MKMapViewDelegate properly (Tested with several delegates) and each of them respond to the change made. 我已经正确地分配了MKMapViewDelegate(已经过几个委托测试),并且每个人都响应所做的更改。 But during this delegate: 但是在此代表期间:

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];

    id<MKAnnotation> mp = [annotationView annotation];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
    [mv setRegion:region animated:TRUE];
    NSLog(@"Test");
}

The console should log that when the annotation is added. 添加注释时,控制台应记录该日志。 But somehow it doesn't run the method above. 但是不知何故,它无法运行上述方法。

This is the main file 这是主文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    //[locationManager startUpdatingLocation];
    [mapView setShowsUserLocation:TRUE];

    [self.window makeKeyAndVisible];

    return YES;
}

I wonder which part is wrong. 我不知道哪一部分错了。 Does the [mapView setShowUserLocation:TRUE] add annotation to the map? [mapView setShowUserLocation:TRUE]是否向地图添加注释? If it is yes, how come the there is no response from the method above? 如果是,那么为什么上面的方法没有响应?

This is because you haven't added an annotation yet! 这是因为您尚未添加注释! The blue indicator that show's the user's location isn't classed as an annotation. 显示用户位置的蓝色指示器未分类为注释。 You can add an annotation by simply dropping a red pin onto the map view (touch and hold the map). 您只需在地图视图上放一个红色的大头针(触摸并按住地图)即可添加注释。

If you want to create your own annotations for the map view programatically, I suggest you start by checking out the section "Annotating the Map" in the MKMapView class reference documentation and looking at the MKAnnotation protocol reference (if you're unfamiliar with protocols I strongly you suggest you read up on those too!) You can use an instance of the class MKPinAnnotationView , which conforms to this protocol, and you can add an annotation using the addAnnotation: instance method of the MKMapView class. 如果要以编程方式为地图视图创建自己的注释,建议您首先查看MKMapView类参考文档中的“注释地图”部分,然后查看MKAnnotation协议参考 (如果您不熟悉I协议,强烈建议您也阅读这些内容!)您可以使用MKPinAnnotationView类的实例,该实例符合该协议,并且可以使用MKMapView类的addAnnotation: instance方法添加注释。

结帐自定义当前位置注释图钉未随核心位置更新我之前在此处回答过类似的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM