2011년 5월 30일 월요일

iphone map 이용하기

1. Map SDK 를 추가하고
2.   xlb   의   map view 에 delegate 를 추가한다.



//
//  SampleDay10ViewController.h
//  SampleDay10
//
//  Created by bit on 11. 5. 30..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import
#import

// delegate  를 추가한다. 
@interface SampleDay10ViewController : UIViewController {

MKMapView *mMap;
}

@property (nonatomic, retain) IBOutlet MKMapView *mMap;


-(IBAction)onViewModeChanged:(id)sender;
-(IBAction)onDropPinClicked:(id)sender;
-(IBAction)onCurrentPositionClicked:(id)sender;


@end



//
//  SampleDay10ViewController.m
//  SampleDay10
//
//  Created by bit on 11. 5. 30..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay10ViewController.h"

@implementation SampleDay10ViewController
@synthesize mMap;




/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

mMap.showsUserLocation = YES;
mMap.delegate = self;
MKCoordinateRegion reg;
reg.center.latitude = 37.56;
reg.center.longitude = 126.96;
reg.span.latitudeDelta = 0.1;
reg.span.longitudeDelta = 0.1;
[mMap setRegion:reg animated:YES];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


-(IBAction)onViewModeChanged:(id)sender{
NSUInteger idx = [(UISegmentedControl*)sender selectedSegmentIndex];
switch (idx) {
case 0:
mMap.mapType = MKMapTypeStandard;
break;
case 1:
mMap.mapType = MKMapTypeSatellite;
break;
case 2:
mMap.mapType = MKMapTypeHybrid;
break;

default:
break;
}
}
-(IBAction)onDropPinClicked:(id)sender{

MKReverseGeocoder *rev = [[MKReverseGeocoder alloc] initWithCoordinate:mMap.centerCoordinate];
rev.delegate = self;
[rev start];
}
-(IBAction)onCurrentPositionClicked:(id)sender{
MKCoordinateRegion reg = mMap.region;
MKUserLocation *userLocation = mMap.userLocation;
reg.center = userLocation.location.coordinate;
[mMap setRegion:reg animated:YES];
}


// google api 성공 일때 
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{
NSLog(@"placemark = %@", placemark);
[mMap addAnnotation:placemark];
[geocoder release];
}


// google api  실패 일때 .. 주소가 없을때 .. 
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(@"%@", [error localizedDescription]);
[geocoder release];
}


-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *viewIdentifier = @"My Annotate";
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifier];
if( pin == nil ){
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIdentifier] autorelease];
pin.animatesDrop = YES;
pin.pinColor = MKPinAnnotationColorPurple;
pin.canShowCallout = YES;
}
else {
pin.annotation = annotation;
}
return pin;
}


@end

댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.