惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
V2EX - 技术
V2EX - 技术
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
美团技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
S
Securelist
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
Security Latest
Security Latest
L
LINUX DO - 最新话题
NISL@THU
NISL@THU
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Y
Y Combinator Blog
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
T
Threatpost
Hugging Face - Blog
Hugging Face - Blog
Scott Helme
Scott Helme
S
SegmentFault 最新的问题
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
小众软件
小众软件
V
Vulnerabilities – Threatpost
J
Java Code Geeks
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
S
Security @ Cisco Blogs
雷峰网
雷峰网
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
Recent Announcements
Recent Announcements
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
T
Troy Hunt's Blog
MyScale Blog
MyScale Blog

博客园 - Edison.Feng

Objective-读Xml [转]Android:使用URL和URLConnection(多线程下载) 【转】httpModules 与 httpHandlers 【转】挣脱浏览器的束缚(2) - 别让脚本引入坏了事 [转]asp.net中利用ashx实现图片防盗链 求任何一个正数的组合,组合的规则是这个数等于1或2的整数幂之和,请列出组合的情况。 C#利用反射获取对象属性的修改情况 C#利用反射获取对象属性值 Clone:Xml序列化反序列克隆对象 [转]Linux slab 分配器剖析 [转]Linux对I/O端口资源的管理 [转]linux内核分析-初始化分析 start_kernel paging_init [转]Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff ) [转]Linux系统内核接收以太帧的处理程序 母牛生小牛(递归) MS SQL:Funcation拆分行 我使用WebService:Function.asmx的惊叹!!!
[转]iPhone Mapkit 之在地图加入坐标点 使用MKAnnotation和MKAnnotationView
Edison.Feng · 2011-04-10 · via 博客园 - Edison.Feng

☉目標:在前一個範例中建立的Google Map上,加上座標點(POIs),當點選座標點會觸發對應的event。
☉步驟說明:
在 地圖上每一個座標點,都是一個MKAnnotationView,也就是UI。而每一個MKAnnotationView都需要有對應的資料 MKAnnotation,這是Protocal,也就是儲存每個座標點所需要用到的資料的地方。因此,我們要先建立一個使用MKAnnotation的 類別。

依照iPhone開發者文件的說明。這個Protocal需要宣告三個屬性和一個初始化方法。三個屬性分別是coordinate、title、subtitle,和一個方法initWithCoords。

下面是MKAnnotation類別的程式碼 POI.h

  1. #import <FOUNDATION foundation.h>   
  2. #import <MAPKIT mapkit.h>   
  3. #import <CORELOCATION corelocation.h>   
  4.   
  5. @interface POI : NSObject <MKANNOTATION> {   
  6.   
  7.     CLLocationCoordinate2D coordinate;   
  8.     NSString *subtitle;   
  9.     NSString *title;   
  10. }   
  11.   
  12. @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;   
  13. @property (nonatomic,retain) NSString *subtitle;   
  14. @property (nonatomic,retain) NSString *title;   
  15.   
  16. -(id) initWithCoords:(CLLocationCoordinate2D) coords;   
  17.   
  18. @end  

下面是MKAnnotation類別的程式碼 POI.m

  1. #import "POI.h"   
  2.   
  3. @implementation POI   
  4.   
  5. @synthesize coordinate,subtitle,title;   
  6.   
  7. - (id) initWithCoords:(CLLocationCoordinate2D) coords{   
  8.        
  9.     self = [super init];   
  10.        
  11.     if (self != nil) {   
  12.            
  13.         coordinate = coords;    
  14.            
  15.     }   
  16.        
  17.     return self;   
  18.        
  19. }   
  20.   
  21. - (void) dealloc   
  22.   
  23. {   
  24.     [title release];   
  25.     [subtitle release];   
  26.     [super dealloc];   
  27. }   
  28.   
  29. @end  

宣告了符合MKAnnotation Protocal的類別後,我們就要在Google Map上建立座標點。在iPhone上顯示Google Map的程式可以參考

使用MKMapView實作Google Map

接下來,
Step(1): 我宣告了一個函式createMapPoint用來建立座標點。在這裡用到了我們在前面宣告的類別POI(這是一個符合MKAnnotation Protocal的類別),我們Create一個POI,接著將座標點所需的經緯度、標題、子標題等訊息都放進去。接著呼叫

  1. [mapView addAnnotation:poi];  

把我們所建立的POI加入地圖(MKMapView)的Annotation集合中。放入集合的只是座標點的資料,這個時候還沒有真正建立座標點

以下是函式createMapPoint的程式碼:

  1. #import "POI.h"   
  2.   
  3. -(void*) createMapPoint:(MKMapView *)mapView coordinateX:(double)coorX coordinateY:(double)coorY   
  4.                   Title:(NSString*)title Subtitle:(NSString*)subtitle{   
  5.        
  6.        
  7.        
  8.     if(mapView!=nil){   
  9.            
  10.         //set POI lat and lng   
  11.         CLLocationCoordinate2D p1;   
  12.         POI *poi;   
  13.            
  14.         if(coorX && coorY){   
  15.                
  16.             p1.latitude=coorX;   
  17.             p1.longitude = coorY;   
  18.             poi = [[POI alloc] initWithCoords:p1];    
  19.                
  20.             if(title!=NULL)   
  21.                 poi.title=title;   
  22.                
  23.             if(subtitle!=NULL)   
  24.                 poi.subtitle=subtitle;   
  25.              mapView.delegate = self;         
  26.             [mapView addAnnotation:poi];   
  27.             [poi release];   
  28.                
  29.         }   
  30.            
  31.     }   
  32.     return NULL;   
  33. }     

Step(2):參考MKMapView的說明文件可以看到viewForAnnotation這個方法,這是MKMapView實際建立座標點的地方。MKMapView類別在render地圖的時候會依照Annotation集合的資料建立座標點。

  1. - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{   
  2.        
  3.     //方法一:using default pin as a PlaceMarker to display on map   
  4.     MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];   
  5.     newAnnotation.pinColor = MKPinAnnotationColorGreen;   
  6.     newAnnotation.animatesDrop = YES;    
  7.     //canShowCallout: to display the callout view by touch the pin   
  8.     newAnnotation.canShowCallout=YES;   
  9.        
  10.     UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];   
  11.     [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];   
  12.     newAnnotation.rightCalloutAccessoryView=button;    
  13.   
  14.     return newAnnotation;   
  15.        
  16.        
  17.     //方法二:using the image as a PlaceMarker to display on map   
  18.     /*  
  19.      MKAnnotationView *newAnnotation=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];  
  20.      newAnnotation.image = [UIImage imageNamed:@"icon.png"];  
  21.      newAnnotation.canShowCallout=YES;  
  22.      return newAnnotation;  
  23.      */  
  24. }   
  25.    

Annotation集合中有幾筆資料vieForAnnotation方法就會被執行幾次。 因此每次viewForAnnotation被執行,我們都要建立一個MKAnnotationView物件的實體,並且return這個實體。 MKMapView接收到MKAnnotationView的實體就會將它顯示在地圖上,這就是我們想要顯示在地圖上的座標點。在上面程式碼中使用了 MKPinAnnotationView這個物件是繼承自MKAnnotationView,作用就是在地圖上顯示一個大頭釘。你可以用

  1. annotationView.pinColor = MKPinAnnotationColorGreen;   

設定大頭釘的顏色,不過只有紅色、紫色、綠色,三種顏色(似乎有點稀少XD)。

  1. newAnnotation.canShowCallout=YES;   

設定在點選大頭釘的時候氣泡視窗是否會談出來。 第10行到第12行動態建立了一個DetailDisclousue類型的按鈕,替這個按鈕設置了一個 UIControlEventTouchUpInside事件,並將它放入氣泡視窗的AccessoryView中。最後,將建立好的座標點回傳給 MapView。 被註解的方法二是直接使用MKAnnotationView建立座標點,並且設置她的image屬性,因此座標點不一定是大頭釘,可以有更多的變化與花樣。

  1. - (void)checkButtonTapped:(id)sender event:(id)event{   
  2.   
  3.     UIAlertView *tmp= [[UIAlertView alloc] initWithTitle:@"訊息!" message:@"Callout測試" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];   
  4.     [tmp show];   
  5.     [tmp release];   
  6. }   

上面是UIControlEventTouchUpInside事件所執行的事件處理常式。當點選DetailDisclousue按鈕後,就會跳出一個alert,表示這個event有正確無誤的被處理。

從這個範例,我們可以看到MKAnnotation和MKAnnotationView之間的關係。

copy from http://blog.finalevil.com/