
//建立MapPin時會呼叫的函式 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation { //判斷Pin如果是目前位置就不修改 if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinView"]; //設定顏色 pinView.pinColor = MKPinAnnotationColorGreen; UIImage *image = [UIImage imageNamed:@"MapPin.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [image release]; //重設圖片大小與座標 imageView.frame = CGRectMake(0, 0, 30, 30); //設定註解內的圖片 pinView.rightCalloutAccessoryView = imageView; [imageView release]; //點擊時是否出現註解 pinView.canShowCallout = YES; //是否可以被拖曳 pinView.animatesDrop = YES; return pinView;
}
使用的圖片記得要放到專案的 Resources 內,註解除了放入圖片也可以放入 UIButton 來製作按鈕事件。
在這裡有一個很重要的地方,如果你已經鍵入上列程式碼,但是地圖上的註解大頭針並沒有變色或樣式,很有可能是沒有將 MapView 的代理指向自己,因此在繪製 MapPin 時才沒有呼叫到此函式,設定方式如下。
@interface MapPinViewController : UIViewController { 並在繪製 MapView 的地方加上以下程式碼。 //將MapView的代理設為自己 map.delegate = self;
沒有留言:
張貼留言