0

I don't need so many pins , they are crowding the area. I used the output of kml parser to draw the route between two points, how can I remove the extra pins between the route. I only need pins for beginning and ending. How do I reduce those excessive red pins\ from KML showing up.

// Locate the path to the route.kml file in the application's bundle
    // and parse it with the KMLParser.
    kml = [[KMLParser parseKMLAtPath:filePath] retain];

    // Add all of the MKOverlay objects parsed from the KML file to the map.
    NSArray *overlays = [kml overlays];
    [map addOverlays:overlays];

    // Add all of the MKAnnotation objects parsed from the KML file to the map.
    NSArray *annotations = [kml points];
    [map addAnnotations:annotations];

    // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKOverlay> overlay in overlays) {
        if (MKMapRectIsNull(flyTo)) {
            flyTo = [overlay boundingMapRect];
        } else {
            flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
        }
    }     //overlay



    for (id <MKAnnotation> annotation in annotations) {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) {
            flyTo = pointRect;
        } else {
            flyTo = MKMapRectUnion(flyTo, pointRect);
        }
    }        ///annotation


    // Position the map so that all overlays and annotations are visible on screen.
    map.visibleMapRect = flyTo;


  }


  - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
   {
    return [kml viewForOverlay:overlay];
    }

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

if ([[annotation title] isEqualToString:@"Destination"])
{
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"greenpin"];
    newAnnotation.pinColor = MKPinAnnotationColorGreen;
    newAnnotation.animatesDrop = YES;
    newAnnotation.canShowCallout = YES;
    return newAnnotation;


}

else if ([[annotation title] isEqualToString:@"Current Location"])
{
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"yellowpin"];
    newAnnotation.pinColor = MKPinAnnotationColorPurple;
    newAnnotation.animatesDrop = YES;
    newAnnotation.canShowCallout = YES;
    return newAnnotation;



}

else {

    return [kml viewForAnnotation:annotation];}
   }
lilzz
  • 5,243
  • 14
  • 59
  • 87

2 Answers2

1

By this code all the annotations in [kml ponits] are added on map.

NSArray *annotations = [kml points];
    [map addAnnotations:annotations];

if you want only first and last annotation then simply add

NSArray *annotations = [kml points];
    [map addAnnotation:[annotations lastObject]];
    [map addAnnotation:[annotations objectAtIndex:0]];
chatur
  • 2,365
  • 4
  • 24
  • 38
0

Check out this link to the Apple WWDC 2011 video, "Visualizing information geographically with MapKit", which shows their preferred design pattern for clustering MKAnnotation objects.

Community
  • 1
  • 1
paul.lander
  • 458
  • 4
  • 5