iOS 大頭針基本使用

一、簡(jiǎn)介

  • 必須理解: 在地圖上操作大頭針,實(shí)際上是控制大頭針數(shù)據(jù)模型

  • 添加大頭針就是添加大頭針數(shù)據(jù)模型

    • 添加方法:直接添加大頭針模型, 然后系統(tǒng)會(huì)添加系統(tǒng)默認(rèn)的大頭針到地圖上
      annotation參數(shù)(需要自定義,遵守MKAnnotation協(xié)議)
      場(chǎng)景1:在地圖中心添加大頭針;
      場(chǎng)景2:鼠標(biāo)點(diǎn)哪,大頭針加哪;(并進(jìn)行反地理編碼設(shè)置大頭針標(biāo)注信息)
  • 刪除大頭針就是刪除大頭針數(shù)據(jù)模型

    • 獲取地圖上大頭針數(shù)據(jù)模型,移除大頭針(在手指移動(dòng)方法中)
  • 設(shè)置大頭針視圖顯示,必須實(shí)現(xiàn)下面方法.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
}
  • 注意:


    注意.png
  • 大頭針視圖有循環(huán)利用機(jī)制

  • 要解決大頭針循環(huán)利用的問(wèn)題

  • 注意:系統(tǒng)的大頭針是不可以修改大頭針圖標(biāo)的,必須自定義大頭針視圖

  • 自定義大頭針視圖要直接或者間接繼承MKAnnotationView類(lèi)

  • 大頭針常用屬性

    • 大頭針圖標(biāo)
    • 設(shè)置是否可以彈框
    • 是否可以拖拽
    • 大頭針偏移量
    • 大頭針左側(cè)與右側(cè)視圖
    • 大頭針詳細(xì)視圖
  • 大頭針常用方法:

    • 1.設(shè)置大頭針視圖
    • 2.改變大頭針拖拽狀態(tài)調(diào)用方法
    • 3.大頭針視圖選中
    • 4.大頭針視圖取消選中

二、使用

  • 1.模擬系統(tǒng)大頭針實(shí)現(xiàn)方案,并對(duì)系統(tǒng)大頭針進(jìn)行部分自定義
    • 彈出標(biāo)注, 修改大頭針顏色, 設(shè)置大頭針從天而降, 設(shè)置大頭針可以被拖拽)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
        {
            if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return nil;
            }
            // 如果此方法返回nil, 就會(huì)使用系統(tǒng)自帶的大頭針視圖
            // 模擬下,返回nil,系統(tǒng)的解決方案
            static NSString *pinId = @"pinID";
            MKPinAnnotationView *pinView = ( MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinId];
            if (pinView == nil) {
                pinView  = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinId];
            }
            pinView.annotation = annotation;
            // 是否顯示標(biāo)注
            pinView.canShowCallout = YES;
            // 設(shè)置大頭針顏色
            pinView.pinColor = MKPinAnnotationColorPurple;
            // 設(shè)置大頭針是否有下落動(dòng)畫(huà)
            pinView.animatesDrop = YES;
            return pinView;
        }
  • 2.自定義大頭針基本使用:
    • 大頭針圖標(biāo),大頭針標(biāo)注,左側(cè)視圖,右側(cè)視圖,詳情視圖,等;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
        {
            if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return nil;
            }
            /**  自定義大頭針-------*/
            static NSString *pinId = @"pinID";
            MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinId];
            if (annoView == nil) {
                annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinId];
            }
            annoView.annotation = annotation;
            annoView.image = [UIImage imageNamed:@"category_5"];
            annoView.canShowCallout = YES;
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"huba.jpeg"]];
            imageView.bounds = CGRectMake(0, 0, 44, 44);
            annoView.leftCalloutAccessoryView = imageView;
            imageView.userInteractionEnabled  = YES;
            UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"eason.jpg"]];
            imageView2.bounds = CGRectMake(0, 0, 44, 44);
            annoView.rightCalloutAccessoryView = imageView2;
            annoView.detailCalloutAccessoryView = [UISwitch new];
            annoView.draggable = YES;
            return annoView;
        }
  • 3.選中,和取消選中大頭針時(shí)的代理方法
// 點(diǎn)擊標(biāo)注
        - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
        {
            NSLog(@"點(diǎn)擊標(biāo)注");
        }
        // 選中大頭針
        - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
        {
            NSLog(@"選中大頭針");
        }
        // 取消選中大頭針
        -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
        {
            NSLog(@"取消選中大頭針");
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容