Monday, May 21, 2012 : Article > iPhone User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
17
ตัวอย่างในครั้งนี้จะเป็นการเขียนโปรแกรมเพื่อติดต่อกับแผนที่ที่ใช้ใน iPhone กันนะครับ

1. สร้าง Project ใหม่ชื่อ MapKitExample โดยเลือก Template เป็น View Base Application

2. ทำการสร้าง Class ใหม่สำหรับ เก็บข้อมูลของตำแหน่งที่ต้องการปักหมุด โดยเลือกเมนู File -> New FIle -> Cocoa touch class -> Objective C class  เลือก Subclass = NSObject จากนั้นให้ตั้งชื่อเป็น myAnnotation
3. ทำการเพิ่ม Framework เข้าไป 2 อัน โดยคลิกเมาส์ขวาที่ Framework -> Add -> Existing Framework จากนั้นให้่เลือก CoreLocation.framework และ MapKit.framework
4. ในไฟล์  MapKitExampleViewController.h ให้เพิ่ม #import <MapKit/MapKit.h>


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

@interface MapKitExampleViewController : UIViewController {

    IBOutlet MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@end
5. เปิดไฟล์ MapKitExampleViewController.xib ใน Folder resources ระบบจะทำการเปิด Interface Builder ให้ทำการลาก Control ของ  MapView จาก Library มาวางใน view ของ window. คลิกที่ File's Owner คลิก icon connection ที่ inspector จากนั้นให้ลาก outlet ชื่อ mapView มาวางที่ view ของ windows ที่เป็น MapView จากนั้นให้ทำการ Save และออกจาก Interface Builder

6. ทำการเปิดไฟล์ MapKitExampleViewController.m และทำการเพิ่ม Code ลงไปตามตัวอย่างด้านล่าง


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    //13.962535, 100.625877
    region.center.latitude = 13.962535 ;
    region.center.longitude = 100.625877;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapView setRegion:region animated:YES];
    
    [mapView setDelegate:self];
    
    myAnnotaton *ann = myAnnotaton alloc init;
    ann.title = @"บ้านเราเอง";
    ann.subtitle = @"อยู่มา 3 ปีแระ แต่ใน map ยังไม่มีบ้านเราเลย";
    ann.coordinate = region.center;
    [mapView addAnnotation:ann];
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *myPin = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *myPinID = @"th.co.bighead.pin";
        myPin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myPinID];
        if ( myPin == nil ) myPin = MKPinAnnotationView alloc
                                          initWithAnnotation:annotation reuseIdentifier:myPinID autorelease

;
       
        myPin.pinColor = MKPinAnnotationColorRed;
        myPin.canShowCallout = YES;
        myPin.animatesDrop = YES;
    }
    else {
        [mapView.userLocation setTitle:@"เราอยู่ตรงนี้"];
    }
    return myPin;
}



7. เปิดไฟล์ myAnnotation.h  ทำการ import "MapKit/MKAnnotation.h" จากนั้นให้ทำการเพิ่ม Code ดังตัวอย่าง

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

@interface myAnnotaton : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end
8. เปิดไฟล์ myAnnotation.m และทำการเพิ่ม Code ดังตัวอย่าง

#import "myAnnotaton.h"


@implementation myAnnotaton
@synthesize coordinate,title,subtitle;

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

@end
Save -> Compile และ Run ได้เลยครับ

สามารถ Download ตัวอย่างได้จาก Attached File ด้านล่างครับ

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi