mapView:didAddAnnotationViews: 没有响应
我目前正在阅读 iPhone 编程 The Big Nerd Ranch Guide,并在第 96 页遇到一些问题。
我已正确分配 MKMapViewDelegate(与多个代表进行了测试),并且每个代表都对所做的更改做出了响应。但在此委托期间:
- (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");
}
控制台应在添加注释时记录该信息。但不知何故它不运行上面的方法。
这是主文件,
- (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;
}
我想知道哪一部分是错误的。 [mapView setShowUserLocation:TRUE] 是否向地图添加注释?如果是的话,为什么上面的方法没有反应呢?
I am currently reading the iPhone Programming The Big Nerd Ranch Guide and encounter some problem on page 96.
I have assigned the MKMapViewDelegate properly (Tested with several delegates) and each of them respond to the change made. 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? If it is yes, how come the there is no response from the method above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您还没有添加注释!显示用户位置的蓝色指示器不属于注释。您只需将红色图钉拖放到地图视图上(触摸并按住地图)即可添加注释。
如果您想以编程方式为地图视图创建自己的注释,我建议您首先查看 MKMapView 类参考文档 并查看 MKAnnotation 协议参考 (如果您不熟悉协议,我强烈建议您也阅读这些协议!)您可以使用类的实例 MKPinAnnotationView,符合该协议,可以使用该协议的
addAnnotation:
实例方法添加注释MKMapView 类。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.检查自定义当前位置注释图钉未更新与核心位置我之前回答过类似的问题
Checkout Custom curent location Annotation pin not updating with core location I answered something similar there before