- (MKAnnotationView *)mapView 没有被调用

发布于 2024-10-19 20:40:43 字数 3783 浏览 2 评论 0原文

在我的代码中, - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation 方法没有被调用。我不知道为什么。有人可以帮我吗?

下面是我的代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@","];
    NSLog(@"pin map");
    if(pinView == nil) 
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];

        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIImage *image = [UIImage imageNamed:@"ann.png"];

        CGRect resizeRect;

        resizeRect.size = image.size;
        CGSize maxSize = CGRectInset(self.view.bounds,
                                     [map annotationPadding],
                                     [map annotationPadding]).size;*/
        maxSize.height -= self.navigationController.navigationBar.frame.size.height + [map calloutHeight];
        if (resizeRect.size.width > maxSize.width)
            resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
        if (resizeRect.size.height > maxSize.height)
            resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

        resizeRect.origin = (CGPoint){0.0f, 0.0f};

        UIGraphicsBeginImageContext(resizeRect.size);
        [image drawInRect:resizeRect];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        pinView.image = resizedImage;
        pinView.opaque = NO;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == mapView.userLocation)
        {

            return nil;
        }
        return pinView;

    } 
    else 
    {

        pinView.annotation = annotation;
    }

    return pinView;

}

这就是我向地图添加注释的方式:

-(void) Annotations:(int)i
{

    /*NSString* address = [mpartyDetail objectAtIndex:i];
    address = [[address componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", address];
    NSLog(@"nsstring %@",urlString);
    NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
    NSLog(@"location string %@",locationString);
    NSArray *latlng = [locationString componentsSeparatedByString:@","];
    NSLog(@"the latlng %@",latlng);

    float lat = [[latlng objectAtIndex:2] floatValue];
    float lng = [[latlng objectAtIndex:3] floatValue]; */
    NSLog(@"ann");
    lat = [[latiArray objectAtIndex:i]floatValue];
    lng = [[longiArray objectAtIndex:i]floatValue]; 
    CLLocationCoordinate2D newCoord = {lat,lng};
    mapAnnotations* annotation = [[mapAnnotations alloc] initWithCoordinate:newCoord];

    [mapView addAnnotation:annotation];

}

mapAnnotations 是另一个类。下面的代码显示了被调用的该类的方法:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{

    NSLog(@"the cor");
    self = [super init];

    if (self != nil) {
        NSLog(@"in");
        _coordinate = coordinate;

    }

    return self;

}

这个想法是,当用户按下地图时我添加注释,并且用户可以为该图钉提供任何标题。我需要一个标注来显示用户输入的标题。正在添加注释。但我无法获得标注,因为没有调用此方法。

In my code, the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation method is not getting called. I don know why. Can anyone please help me?

Below is my code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@","];
    NSLog(@"pin map");
    if(pinView == nil) 
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];

        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIImage *image = [UIImage imageNamed:@"ann.png"];

        CGRect resizeRect;

        resizeRect.size = image.size;
        CGSize maxSize = CGRectInset(self.view.bounds,
                                     [map annotationPadding],
                                     [map annotationPadding]).size;*/
        maxSize.height -= self.navigationController.navigationBar.frame.size.height + [map calloutHeight];
        if (resizeRect.size.width > maxSize.width)
            resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
        if (resizeRect.size.height > maxSize.height)
            resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

        resizeRect.origin = (CGPoint){0.0f, 0.0f};

        UIGraphicsBeginImageContext(resizeRect.size);
        [image drawInRect:resizeRect];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        pinView.image = resizedImage;
        pinView.opaque = NO;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == mapView.userLocation)
        {

            return nil;
        }
        return pinView;

    } 
    else 
    {

        pinView.annotation = annotation;
    }

    return pinView;

}

This is how I am adding the annotations to the map:

-(void) Annotations:(int)i
{

    /*NSString* address = [mpartyDetail objectAtIndex:i];
    address = [[address componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", address];
    NSLog(@"nsstring %@",urlString);
    NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
    NSLog(@"location string %@",locationString);
    NSArray *latlng = [locationString componentsSeparatedByString:@","];
    NSLog(@"the latlng %@",latlng);

    float lat = [[latlng objectAtIndex:2] floatValue];
    float lng = [[latlng objectAtIndex:3] floatValue]; */
    NSLog(@"ann");
    lat = [[latiArray objectAtIndex:i]floatValue];
    lng = [[longiArray objectAtIndex:i]floatValue]; 
    CLLocationCoordinate2D newCoord = {lat,lng};
    mapAnnotations* annotation = [[mapAnnotations alloc] initWithCoordinate:newCoord];

    [mapView addAnnotation:annotation];

}

mapAnnotations is another class. The below code shows the method of that class which is getting called:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{

    NSLog(@"the cor");
    self = [super init];

    if (self != nil) {
        NSLog(@"in");
        _coordinate = coordinate;

    }

    return self;

}

The idea is that I add annotations when the user presses on the map and the user can give any title to that pin. I need a callout to show the title entered by the user. The annotations are getting added. But I am not able to get the callout as this method is not getting called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情愿 2024-10-26 20:40:43

您是否将包含该方法的类指定为地图视图的委托,是否实际上向地图视图添加了任何注释,以及这些注释的位置在当前显示在屏幕上的地图部分中是否实际可见?

Did you assign the class containing that method as the delegate for your map view, did you actually add any annotations to the map view, and are the locations for those annotations actually visible in the portion of the map currently displayed on-screen?

晒暮凉 2024-10-26 20:40:43

正如有人正确指出的那样,在我的例子中,代表没有设置为同一类别
我所做的只是在代码中添加注释,我编写了代码:

mapView.delegate=self;

它对我来说非常有用。
如果你没有这样做,那就去做吧,它可能对你也有用。

as someone rightly pointed out the delegate wasnt set to the same class in my case
all i did was in the code where i add annotation i wrote the code:

mapView.delegate=self;

and it worked great for me.
if you didnt do that do it and it might work for you also droid.

善良天后 2024-10-26 20:40:43

您如何向地图添加注释?我用以下

while(some condition){
MapAnnotation *annotation = [[MapAnnotation alloc] initWithLatitude:(float)goLat Longitude:(float)goLng title:(NSString*)recallTask.blurb subtitle:(NSString*)recallTask.location];
        [appDelegate.annArray addObject:annotation];
        [annotation release];
        annotation = nil;
    }
[self.mapView addAnnotations:appDelegate.annArray];

How are you adding annotations to your map? I use the following

while(some condition){
MapAnnotation *annotation = [[MapAnnotation alloc] initWithLatitude:(float)goLat Longitude:(float)goLng title:(NSString*)recallTask.blurb subtitle:(NSString*)recallTask.location];
        [appDelegate.annArray addObject:annotation];
        [annotation release];
        annotation = nil;
    }
[self.mapView addAnnotations:appDelegate.annArray];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文