如何在地图中显示多个图钉注释?

时间:2011-05-07 05:42:11

标签: iphone

我静态地显示了一个引脚注释,但我想动态地在地图上显示标题和副标题的许多引脚。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

我有一个字典数组有两个对象lat,很长,我正在这样做 -

    if ([resultArray count])
        {
            for (int i =0; i < [resultArray count]; i++) 
            {
                NSDictionary *dict  = [resultArray objectAtIndex:i];

                MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
                region.center.latitude = [[dict objectForKey:@"lat"] floatValue];
                region.center.longitude = [[dict objectForKey:@"long"] floatValue];
                region.span.longitudeDelta = 70.0f;
                region.span.latitudeDelta = 70.0f;

                MyAnnotations *ann = [[MyAnnotations alloc] init];
                ann.title = @"title";
                ann.coordinate = region.center;
                [mapView addAnnotation:ann];
            }
        }

    //
    //  MyAnnotation.h
    //

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>

    @interface MyAnnotation : NSObject <MKAnnotation> {

        CLLocationCoordinate2D coordinate;
        NSString *title;
        NSString *subtitle;

    }
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;


    @end


//
//  MyAnnotation.m
//

#import "MyAnnotation.h"


@implementation MyAnnotation
@synthesize coordinate,title,subtitle;

-(void)dealloc
{
    [title release];
    [subtitle release];
    [super dealloc];
}

@end

将此委托用于标注 -

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
           pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    [defaultPinID release];

    return pinView;
}