点击叠加时显示标注和图钉

发布于 2024-12-21 05:35:50 字数 2121 浏览 2 评论 0原文

我试图在用户点击叠加层时显示标注。然后,标注上会有一个基于所选叠加层的标题。我希望仅当用户点击叠加层时才显示注释。但问题是覆盖层无法识别点击,并且所有注释在开始时都是可见的。我想让它们隐藏起来。

这里有一个类似的问题。但我不明白。 点击覆盖时显示标注

覆盖协调从服务器下载并添加如下:

 //Add a polygon
        MKPolygon *rect=[MKPolygon polygonWithCoordinates:parkingCords count:5];
        [self.mapView addOverlay:rect];
        [self.mapView addAnnotation:rect];

每个覆盖层现在在其中心有一个注释。

ViewForAnnotation

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

...
 else if([annotation isKindOfClass:[MKPolygon class]]){
        NSLog(@"MKPOLYGON CLASS");
        static NSString *identifier3 = @"else";  
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier3];


        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier3];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;

        } else {
            annotationView.annotation = annotation;

        } 
...
}

viewForOverlay

 -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
        if([overlay isKindOfClass:[MKPolygon class]]){
            MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
            view.lineWidth=1;
            view.strokeColor=[UIColor blueColor];
            view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.3];

            recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTapped)]; 
            recognizer.delegate=self;
            [view addGestureRecognizer:recognizer];
            [recognizer release];

            return view;
        }
        return nil;
    }


-(void)overlayTapped{

    NSLog(@"overlay tapped");
    //[self.mapView setSelectedAnnotations:?????];
}

Im trying to show a callout when user taps on an overlay. The callout then has a title on it based on the selected overlay. I want the annotations to be shown only when users taps on an overlay. but the problem is that the overlay doesnt recognize the tap and all the annotations are visible at start. I want them hidden.

a similiar question is here. but I cant figure it out.
Show callout when tapping overlay

overlays coordinations are downloaded from the server and added like this:

 //Add a polygon
        MKPolygon *rect=[MKPolygon polygonWithCoordinates:parkingCords count:5];
        [self.mapView addOverlay:rect];
        [self.mapView addAnnotation:rect];

Each overlay has now an Annotation in its centre.

ViewForAnnotation

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

...
 else if([annotation isKindOfClass:[MKPolygon class]]){
        NSLog(@"MKPOLYGON CLASS");
        static NSString *identifier3 = @"else";  
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier3];


        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier3];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;

        } else {
            annotationView.annotation = annotation;

        } 
...
}

viewForOverlay

 -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
        if([overlay isKindOfClass:[MKPolygon class]]){
            MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
            view.lineWidth=1;
            view.strokeColor=[UIColor blueColor];
            view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.3];

            recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTapped)]; 
            recognizer.delegate=self;
            [view addGestureRecognizer:recognizer];
            [recognizer release];

            return view;
        }
        return nil;
    }


-(void)overlayTapped{

    NSLog(@"overlay tapped");
    //[self.mapView setSelectedAnnotations:?????];
}

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

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

发布评论

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

评论(1

晨曦÷微暖 2024-12-28 05:35:50

我已经解决了这段代码:

在确定跨度区域之后

 CLLocation *userLoc = myMapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);
    myMapView.delegate=self;


    NSMutableArray* annotations=[[NSMutableArray alloc] init];

    //vallee
    CLLocationCoordinate2D theCoordinate1;
    theCoordinate1.latitude = 45.752453;
    theCoordinate1.longitude = 7.642313;
    //venezia
    CLLocationCoordinate2D theCoordinate2;
    theCoordinate2.latitude = 45.442793;
    theCoordinate2.longitude = 12.329691;
    //campione
    CLLocationCoordinate2D theCoordinate3;
    theCoordinate3.latitude = 45.971027;
    theCoordinate3.longitude = 8.971362;
    //sanremo
    CLLocationCoordinate2D theCoordinate4;
    theCoordinate4.latitude = 43.814910;
    theCoordinate4.longitude = 7.772050;

    myAnnotation* myAnnotation1=[[myAnnotation alloc] init];

    myAnnotation1.coordinate=theCoordinate1;
    myAnnotation1.title=@"Casinò della Vallée";
    myAnnotation1.subtitle=@"Saint Vincent";

    myAnnotation* myAnnotation2=[[myAnnotation alloc] init];

    myAnnotation2.coordinate=theCoordinate2;
    myAnnotation2.title=@"Casinò di Venezia";
    myAnnotation2.subtitle=@"Venezia";

    myAnnotation* myAnnotation3=[[myAnnotation alloc] init];

    myAnnotation3.coordinate=theCoordinate3;
    myAnnotation3.title=@"Casinò di Campione";
    myAnnotation3.subtitle=@"Campione";

    myAnnotation* myAnnotation4=[[myAnnotation alloc] init];

    myAnnotation4.coordinate=theCoordinate4;
    myAnnotation4.title=@"Casinò di Sanremo";
    myAnnotation4.subtitle=@"Sanremo";


    [myMapView addAnnotation:myAnnotation1];
    [myMapView addAnnotation:myAnnotation2];
    [myMapView addAnnotation:myAnnotation3];
    [myMapView addAnnotation:myAnnotation4];


    //[annotations addObject:myAnnotation1];
    [annotations addObject:myAnnotation2];
    [annotations addObject:myAnnotation3];
    [annotations addObject:myAnnotation4];


    NSLog(@"%d",[annotations count]);



}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
    if (annotation == mapView.userLocation) {
        return nil;
    }
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
    pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.canShowCallout = YES;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    pinView.animatesDrop = YES;
    return pinView;
}

//disclosure button con indicazioni
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];

      //street from here to there

    NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];

        NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
        [[UIApplication sharedApplication] openURL:url];



}

希望这段代码有用

I've solved whit this code:

After you had determinate the span region

 CLLocation *userLoc = myMapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);
    myMapView.delegate=self;


    NSMutableArray* annotations=[[NSMutableArray alloc] init];

    //vallee
    CLLocationCoordinate2D theCoordinate1;
    theCoordinate1.latitude = 45.752453;
    theCoordinate1.longitude = 7.642313;
    //venezia
    CLLocationCoordinate2D theCoordinate2;
    theCoordinate2.latitude = 45.442793;
    theCoordinate2.longitude = 12.329691;
    //campione
    CLLocationCoordinate2D theCoordinate3;
    theCoordinate3.latitude = 45.971027;
    theCoordinate3.longitude = 8.971362;
    //sanremo
    CLLocationCoordinate2D theCoordinate4;
    theCoordinate4.latitude = 43.814910;
    theCoordinate4.longitude = 7.772050;

    myAnnotation* myAnnotation1=[[myAnnotation alloc] init];

    myAnnotation1.coordinate=theCoordinate1;
    myAnnotation1.title=@"Casinò della Vallée";
    myAnnotation1.subtitle=@"Saint Vincent";

    myAnnotation* myAnnotation2=[[myAnnotation alloc] init];

    myAnnotation2.coordinate=theCoordinate2;
    myAnnotation2.title=@"Casinò di Venezia";
    myAnnotation2.subtitle=@"Venezia";

    myAnnotation* myAnnotation3=[[myAnnotation alloc] init];

    myAnnotation3.coordinate=theCoordinate3;
    myAnnotation3.title=@"Casinò di Campione";
    myAnnotation3.subtitle=@"Campione";

    myAnnotation* myAnnotation4=[[myAnnotation alloc] init];

    myAnnotation4.coordinate=theCoordinate4;
    myAnnotation4.title=@"Casinò di Sanremo";
    myAnnotation4.subtitle=@"Sanremo";


    [myMapView addAnnotation:myAnnotation1];
    [myMapView addAnnotation:myAnnotation2];
    [myMapView addAnnotation:myAnnotation3];
    [myMapView addAnnotation:myAnnotation4];


    //[annotations addObject:myAnnotation1];
    [annotations addObject:myAnnotation2];
    [annotations addObject:myAnnotation3];
    [annotations addObject:myAnnotation4];


    NSLog(@"%d",[annotations count]);



}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
    if (annotation == mapView.userLocation) {
        return nil;
    }
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
    pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.canShowCallout = YES;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    pinView.animatesDrop = YES;
    return pinView;
}

//disclosure button con indicazioni
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];

      //street from here to there

    NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];

        NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
        [[UIApplication sharedApplication] openURL:url];



}

Wishing this code useful

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