160 likes | 277 Vues
This guide provides a step-by-step approach to building an iPhone application that utilizes MapKit to display multiple coordinate points on a map. It focuses on using Objective-C for iOS SDK version 3.0. The tutorial covers initializing the MKMapView, setting up MKAnnotation for individual coordinate points, and rendering them on the map with customizable annotation views. Discover how to manage map regions, create annotations, and integrate user interaction with annotations for a dynamic mapping experience.
E N D
目標:建立一個Google Map畫面,並可以同時顯示多點座標資料。 • 語言:Objective C • 限制:iPhone的作業系統需為3.0版本,開發使用的SDK也要是3.0。
Framework • 加入Mapkit Framework • import <MapKit/MKMapView.h> <MapKit/MKAnnotation.h> <MapKit/MKAnnotationView.h>
View • 加入ViewController
Map • 在viewDidLoad方法中載入地圖。map = [[MKMapViewalloc] initWithFrame:rect]; • MKCoordinateRegion表示座標區域(地圖上的某個區域),其中包含兩個重要的屬性,Center和Span。 • Center表示這塊區域的中心位置。Span表示這塊區域由中心往外延伸的距離。
CLLocationCoordinate2D theCoordinate;//指定變數theCoordinate.latitude=22.999018;theCoordinate.longitude=120.220910;//設定座標 經緯度 • MKCoordinateRegiontheRegin;CLLocationCoordinate2D theCenter;theCenter.latitude=22.999018;theCenter.longitude= 120.220910;//設定中央座標theRegin.center=theCenter;MKCoordinateSpantheSpan;theSpan.latitudeDelta= 0.009;theSpan.longitudeDelta= 0.009;//延伸的距離theRegin.span = theSpan; • [map setRegion:theRegin];//設定區域[map regionThatFits:theRegin];[view addSubview:map];//加入地圖顯示
Point • 地圖上每一個座標點,都是一個MKAnnotationView,也就是UI。 • 每一個MKAnnotationView都需要有對應的資料MKAnnotation,這是Protocol,也就是儲存每個座標點所需要用到的資料的地方。 • 建立一個使用MKAnnotation的類別。
Point • 三個屬性:coordinate、title、subtitle一個方法:initWithCoordinate。 • @interface MapAnnotation : NSObject <MKAnnotation>{CLLocationCoordinate2Dcoordinate;NSString*title;NSString*subtitle;} • -initWithCoordinate:(CLLocationCoordinate2D)coords
Point • 宣告函式createMapPoint建立座標資料。呼叫[mapViewaddAnnotation:mapannotation];將mapannotation的資料加入地圖的Annotation集合中*座標點尚未建立
CLLocationCoordinate2D p1;MapAnnotation *mapannotation;if (coorX && coorY ){ p1.latitude=coorX; p1.longitude = coorY;mapannotation = [[MapAnnotationalloc] initWithCoordinate:p1]; if(title!=NULL)mapannotation.title=title; if(subtitle!=NULL)mapannotation.subtitle=subtitle; [mapViewaddAnnotation:mapannotation];}
Point • 方法 viewForAnnotation建立實體點 • MKMapView類別會在呈現地圖的時候依照Annotation集合的資料建立座標點。Annotation集合中有幾筆資料viewForAnnotation方法就會被執行幾次。 • (MKAnnotationView *) mapView:(MKMapView *)mapViewviewForAnnotation:(id <MKAnnotation>) annotation
using the image as a PlaceMarker to display on map MKAnnotationView *newAnnotation=[[MKAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:@"annotation1"]; newAnnotation.image = [UIImageimageNamed:@"image.png"]; newAnnotation.canShowCallout=YES; returnnewAnnotation;
using default pin as a PlaceMarker to display on map newAnnotation.pinColor = MKPinAnnotationColorGreen; newAnnotation.animatesDrop = YES; newAnnotation.canShowCallout=YES; //氣泡視窗UIButton *button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure]; [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; newAnnotation.rightCalloutAccessoryView=button;
Call • 使用程式 [createMapPoint:mapcoordinateX:22.995465 coordinateY:120.219728 Title:@“人物J” Subtitle:@“人物所在地”];設定點的座標及屬性,並以以下程式呼叫[mapViewaddAnnotation:mapannotation];在viewForAnnotation Event中就會render這些座標點。
Image Pin