1 / 16

iPhone

iPhone. 目標:建立一個 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.

Télécharger la présentation

iPhone

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. iPhone

  2. 目標:建立一個Google Map畫面,並可以同時顯示多點座標資料。 • 語言:Objective C • 限制:iPhone的作業系統需為3.0版本,開發使用的SDK也要是3.0。

  3. Framework • 加入Mapkit Framework • import <MapKit/MKMapView.h> <MapKit/MKAnnotation.h> <MapKit/MKAnnotationView.h>

  4. View • 加入ViewController

  5. Map • 在viewDidLoad方法中載入地圖。map = [[MKMapViewalloc] initWithFrame:rect]; • MKCoordinateRegion表示座標區域(地圖上的某個區域),其中包含兩個重要的屬性,Center和Span。 • Center表示這塊區域的中心位置。Span表示這塊區域由中心往外延伸的距離。

  6. 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];//加入地圖顯示

  7. Point • 地圖上每一個座標點,都是一個MKAnnotationView,也就是UI。 • 每一個MKAnnotationView都需要有對應的資料MKAnnotation,這是Protocol,也就是儲存每個座標點所需要用到的資料的地方。 • 建立一個使用MKAnnotation的類別。

  8. Point • 三個屬性:coordinate、title、subtitle一個方法:initWithCoordinate。 • @interface MapAnnotation : NSObject <MKAnnotation>{CLLocationCoordinate2Dcoordinate;NSString*title;NSString*subtitle;} • -initWithCoordinate:(CLLocationCoordinate2D)coords

  9. Point • 宣告函式createMapPoint建立座標資料。呼叫[mapViewaddAnnotation:mapannotation];將mapannotation的資料加入地圖的Annotation集合中*座標點尚未建立

  10. 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];}

  11. Point • 方法 viewForAnnotation建立實體點 • MKMapView類別會在呈現地圖的時候依照Annotation集合的資料建立座標點。Annotation集合中有幾筆資料viewForAnnotation方法就會被執行幾次。 • (MKAnnotationView *) mapView:(MKMapView *)mapViewviewForAnnotation:(id <MKAnnotation>) annotation

  12. 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;      

  13. 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; 

  14. Call • 使用程式 [createMapPoint:mapcoordinateX:22.995465 coordinateY:120.219728 Title:@“人物J” Subtitle:@“人物所在地”];設定點的座標及屬性,並以以下程式呼叫[mapViewaddAnnotation:mapannotation];在viewForAnnotation Event中就會render這些座標點。

  15. Image Pin

  16. ~The End~

More Related