mapView:didAddAnnotationViews: 没有响应

发布于 2024-10-20 18:03:23 字数 1179 浏览 1 评论 0原文

我目前正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-10-27 18:03:23

这是因为您还没有添加注释!显示用户位置的蓝色指示器不属于注释。您只需将红色图钉拖放到地图视图上(触摸并按住地图)即可添加注释。

如果您想以编程方式为地图视图创建自己的注释,我建议您首先查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文