Integrasi MapKit dengan Location

Posted: August 27, 2010 in iPhone Programming

Pada kali ini, saya akan berbagi suatu teknik untuk integrasi Map Kit dengan Location

Langkah pertama, buat project dengan nama MapKit, kemudian import framework : MapKit.framework.

MapKit.framework ada di path ini : /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/System/Library/Frameworks/

Kemudian buat MapAnnotation dengan code seperti ini :
MapAnnotation.h


#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject <MKAnnotation>

{

CLLocationCoordinate2D coordinate;

NSString *title;

NSString *subtitle;

NSString *urlstring;

NSString *picstring;

}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic, retain) NSString *title;

@property (nonatomic, retain) NSString *subtitle;

@property (nonatomic, retain) NSString *urlstring;

@property (nonatomic, retain) NSString *picstring;

@end

MapAnnotation.m


#import "MapAnnotation.h"

@implementation MapAnnotation

@synthesize coordinate;

@synthesize title;

@synthesize subtitle;

@synthesize urlstring;

@synthesize picstring;

- (id) initWithCoordinate: (CLLocationCoordinate2D) aCoordinate

{

if (self = [super init]) coordinate = aCoordinate;

return self;

}

-(void) dealloc

{

self.title = nil;

self.subtitle = nil;

self.urlstring = nil;

self.picstring = nil;

[super dealloc];

}

@end

Setelah itu untuk Mapnya, kira-kira fungsinya seperti ini :


#pragma mark logic Button

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

{

MapAnnotation *annotation = view.annotation;

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:annotation.urlstring]];

}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

{

if([views count] > 1)

{

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

{

MKPinAnnotationView *mkaview = [views objectAtIndex:i];

NSLog(@"%@\n",[(MapAnnotation *) mkaview.annotation picstring]);

mkaview.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

UIImage *origimage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:[(MapAnnotation *) mkaview.annotation picstring]]]];

UIGraphicsBeginImageContext(CGSizeMake(32.0f, 32.0f));

[origimage drawInRect:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

mkaview.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:img] autorelease];

}

}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

NSArray * arrLati = [NSArray arrayWithObjects:

@"-6.600486",

@"-6.200486",

@"-5.800486",

@"-5.400486",

nil];

NSArray *arrLongi = [NSArray arrayWithObjects:

@"106.797743",

@"106.797743",

@"106.797743",

@"106.797743",

nil];

NSArray *namaImg = [NSArray arrayWithObjects:

@"http://www.boston.com/news/globe/ideas/brainiac/pikachu.gif",

@"http://www.gearfuse.com/wp-content/uploads/2010/07/Apple.jpg",

@"http://macclubindonesia.com/forums/image.php?u=27118&dateline=1259138986",

@"http://macclubindonesia.com/forums/image.php?u=27118&dateline=1259138986",

nil];

mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];

mapView.showsUserLocation=NO;

mapView.mapType=MKMapTypeHybrid;

annotationDict = [[NSMutableDictionary alloc] init];

//definir le zoom

MKCoordinateSpan span;

span.latitudeDelta=0.5;

span.longitudeDelta=0.5;

double latitude = [[NSString stringWithFormat:@"%@",[arrLati objectAtIndex:0]] doubleValue];

double longitude =[[NSString stringWithFormat:@"%@",[arrLongi objectAtIndex:0]] doubleValue];

CLLocationCoordinate2D parisCoordinates;

parisCoordinates.latitude=latitude;

parisCoordinates.longitude=longitude;

MKCoordinateRegion parisRegion;

parisRegion.span=span;

parisRegion.center=parisCoordinates;

[mapView setRegion:parisRegion animated:TRUE];

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

{

latitude = [[NSString stringWithFormat:@"%@",[arrLati objectAtIndex:i]] doubleValue];

longitude =[[NSString stringWithFormat:@"%@",[arrLongi objectAtIndex:i]] doubleValue];

CLLocationCoordinate2D p2;

p2.latitude=latitude;

p2.longitude=longitude;

MapAnnotation *annotation = [[[MapAnnotation alloc] initWithCoordinate:p2] autorelease];

annotation.title = [NSString stringWithFormat:@"Testing ke %d",i];

annotation.urlstring = @"http://google.co.id/";

annotation.picstring = [namaImg objectAtIndex:i];

annotation.subtitle = [NSString stringWithFormat:@"%f, %f", p2.latitude, p2.longitude];

[annotationDict setObject:annotation forKey:[NSString stringWithFormat:@"Testing ke %d",i]];

}

[mapView removeAnnotations:mapView.annotations];

[mapView addAnnotations:[annotationDict allValues]];

[self.view addSubview:mapView];

mapView.delegate = self;

[super viewDidLoad];

}

}
<div>

Jangan lupa tambahkan : #import <MapKit/MapKit.h> dan  #import "AnnotationMap.h"
Saya akan menampilkan contoh source code dari project ini :
Screenshot :

Map iPhone

Map iPhone

Advertisement
Comments
  1. josy says:

    I tried reaching you for a job in Singapore.
    Please let me know your contact number with coutry code.
    Also please reach me at +65-96484990

    Thanks

    Josy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s