how to add multiple pin at the same lat/long

血红的双手。 提交于 2019-12-25 00:59:00

问题


I am trying to add multiple pin at the same location.

for (int i = 0; i < [arrListing count]; i++) {


            List *obj = [arrListing objectAtIndex:i];
            NSLog(@"Title %@",obj.Title);

            CLLocationCoordinate2D annotationCoord;

            annotationCoord.latitude = [obj.lat floatValue];
            annotationCoord.longitude = [obj.log floatValue];

            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = annotationCoord;
            annotationPoint.title = obj.Title;

            [mapView addAnnotation:annotationPoint];
        }

the above code represent adding mulitiple annotation but many pin are at the same location

So I can see only. last and the second last at that point. below is the code for the viewForAnnotation

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
     MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annView"];

    if (!annView) {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annView"];
        annView.pinColor = MKPinAnnotationColorRed;
        annView.animatesDrop = YES;
        annView.canShowCallout = YES;
        NSLog(@"iRow :%d",iRow);
        annView.tag = iRow++;
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.rightCalloutAccessoryView = rightButton;
        NSLog(@"if condition");
    }
    else
    {
        annView.annotation = annotation;
        NSLog(@"else condition");
    }
    return annView;
}

回答1:


-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    if(annotation != map.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    }
    else {
        [map.userLocation setTitle:@"I am here"];
    }
    return pinView;
}


来源:https://stackoverflow.com/questions/13862775/how-to-add-multiple-pin-at-the-same-lat-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!