开发者

How to show multiple pin annotation in map?

开发者 https://www.devze.com 2023-03-04 12:52 出处:网络
I m showing a pin annotation statically, but i want to do it dynamically to show a lot of pins on map with title and subtitle.

I m showing a pin annotation statically, but i want to do it dynamically to show a lot of pins on map with title and subtitle.

Thx in advance开发者_Go百科.


I have an array of dictionaries with have two objects lat, long and i am doing this -

    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

use this delegate for callout -

- (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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消