十年專注于品牌網站建設 十余年專注于網站建設_小程序開發_APP開發,低調、敢創新、有情懷!
      南昌百恒網絡微信公眾號 掃一掃關注
      小程序
      tel-icon全國服務熱線:400-680-9298,0791-88117053
      掃一掃關注百恒網絡微信公眾號
      掃一掃打開百恒網絡微信小程序

      百恒網絡

      南昌百恒網絡

      實現地圖委托方法mapView:viewForAnnotation:

      百恒網絡 2017-09-19 5698

      昨天南昌APP制作開發公司-百恒網絡為大家介紹了使用IOS蘋果地圖添加標注的第一個步驟:觸發添加動作,那么今天本公司就來為大家介紹第二步:實現地圖委托方法 mapView:viewForAnnotation:,這也是最后一步!具體方法如下:

      MKMapViewDelegate委托協議方法mapView:viewForAnnotation:的代碼如下:

      func mapView(mapView: MKMapView!, viewForAnnotation annotation:

      MKAnnotation!) -> MKAnnotationView! { ①

      var annotationView = self.mapView.dequeueReusableAnnotationViewWith

      Identifier("PIN_ANNOTATION")

      as? MKPinAnnotationView ②

      if annotationView == nil { ③

      annotationView = MKPinAnnotationView(annotation: annotation,

      reuseIdentifier: "PIN_ANNOTATION") ④

      }

      annotationView!.pinColor = MKPinAnnotationColor.Purple ⑤

      annotationView!.animatesDrop = true ⑥

      annotationView!.canShowCallout = true ⑦

      return annotationView!

      }

      - (MKAnnotationView *) mapView:(MKMapView *)theMapView

      viewForAnnotation:(id ) annotation { ①

      MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView

      dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"]; ②

      if(annotationView == nil) { ③

      annotationView = [[MKPinAnnotationView alloc]

      initWithAnnotation: annotation

      reuseIdentifier:@"PIN_ANNOTATION"]; ④

      }

      annotationView.pinColor = MKPinAnnotationColorPurple; ⑤

      annotationView.animatesDrop = YES; ⑥

      annotationView.canShowCallout = YES; ⑦

      return annotationView;

      }

      在上述代碼中,第①行代碼所示的委托方法mapView:viewForAnnotation:在地圖視圖添加標注時回調。給地圖視圖添加標注的方法是self.mapView.addAnnotation(annotation),其中annotation是地圖標注對象。

      第②~④行代碼用于獲得地圖標注對象MKPinAnnotationView,其中采用了可重用MKPinAnnotationView對象設 計。這里使用可重用對象,是為了節約內存。一般情況下,盡可能使用已有對象,減少實例化對象。首先,在第 ②行代碼中,我們使用dequeueReusableAnnotationViewWithIdentifier:方法通過一個可重用標識符PIN_ANNOTATION 獲得MKPinAnnotationView對象,如果這個對象不存在(第③行代碼判斷是否存在),則需要使用第④行代碼的 initWithAnnotation:reuseIdentifier:構造器創建,其中reuseIdentifier參數是可重用標識符。

      第⑤行代碼設置大頭針標注視圖的顏色為紫色。此外,該顏色還可以設置成紅色(Swift版MKPinAnnotation- Color.Red,Objective-C版使用MKPinAnnotationColorRed)和綠色(Swift版使用MKPinAnnotationColor.Green, Objective-C版使用MKPinAnnotationColorGreen)。

      第⑥行代碼說明設置標注視圖時,是否以動畫效果的形式顯示在地圖上。第⑦行代碼用于在標注點上顯示一 些附加信息。如果canShowCallout為true(或YES),則點擊“大頭針”頭時,會出現一個氣泡(如圖1所示), 而氣泡中的文字信息封裝在MyAnnotation對象中,其中第一行文字(大一點的文字)保存在title屬性中,而第二 行文字(小一點的文字)保存在subtitle屬性中。

      canShowCallout設置為true的情況

      圖1 canShowCallout設置為true的情況

      在委托方法的后,返回annotationView標注點視圖對象。

      最后,我們看看自定義標注類MyAnnotation。MyAnnotation的定義如下:

      import MapKit

      class MyAnnotation: NSObject, MKAnnotation{

      //街道信息屬性

      var streetAddress: String!

      //城市信息屬性

      var city: String!

      //州、省、市信息

      var state: String!

      //郵編

      var zip: String!

      //地理坐標

      var coordinate: CLLocationCoordinate2D

      init(coordinate: CLLocationCoordinate2D) {

      self.coordinate = coordinate

      }

      var title: String {

      return "您的位置!"

      }

      var subtitle: String {

      var res = NSMutableString()

      if (self.state != nil) {

      res.appendFormat("%@", self.state)

      }

      if (self.city != nil) {

      res.appendFormat(" ? %@", self.state)

      }

      if (self.zip != nil) {

      res.appendFormat(" ? %@", self.zip)

      }

      if (self.streetAddress != nil) {

      res.appendFormat(" ? %@", self.streetAddress)

      }

      return res

      }

      }

      //MKAnnotation.h文件

      #import

      @interface MKAnnotation : NSObject

      @property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

      //街道信息屬性

      @property (nonatomic, copy) NSString *streetAddress;

      //城市信息屬性

      @property (nonatomic, copy) NSString *city;

      //州、省、市信息

      @property (nonatomic, copy) NSString *state;

      //郵編

      @property (nonatomic, copy) NSString *zip;

      //地理坐標

      @property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

      @end

      //MKAnnotation.m文件

      #import "MKAnnotation.h"

      @implementation MKAnnotation

      - (NSString *)title {

      return @"您的位置!";

      }

      - (NSString *)subtitle {

      NSMutableString *ret = [NSMutableString new];

      if (_state)

      [ret appendString:_state];

      if (_city)

      [ret appendString:_city];

      if (_city && _state)

      [ret appendString:@", "];

      if (_streetAddress && (_city || _state || _zip))

      [ret appendString:@" ? "];

      if (_streetAddress)

      [ret appendString:_streetAddress];

      if (_zip)

      [ret appendFormat:@", %@", _zip];

      return ret; }

      @end

      地圖上的標注點類必須實現MKAnnotation協議。MKAnnotation協議需要重寫如下兩個屬性。 ?

      1、title:標注點上的主標題。

      2、subtitle:標注點上的副標題。

      在重寫subtitle屬性時,我們將它的相關信息拼接成字符串賦值給它。這里,我們可以根據自己的需要和習慣拼接在這個字符串的前后。

      好了,關于在南昌APP開發中實現在地圖視圖上添加標注點的方法就已經介紹完了,如果大家對于第一個步驟:《使用IOS蘋果地圖添加標注之觸發添加動作》的方法還不太理解的話,可點擊再重新看一遍,或者來電咨詢百恒網絡,我們專業為您解答!


      400-680-9298,0791-88117053
      掃一掃關注百恒網絡微信公眾號
      掃一掃打開百恒網絡小程序

      歡迎您的光顧,我們將竭誠為您服務×

      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售后服務 售后服務
       
      售后服務 售后服務
       
      備案專線 備案專線
       
      ×
      亚洲精品白浆高清久久久久久| 亚洲日韩国产AV无码无码精品| 精品无码人妻一区二区免费蜜桃| 好属妞这里只有精品久久| 精品国产91久久久久久久a| 青娱乐精品视频在线观看| 无码区日韩特区永久免费系列| 国产aⅴ精品一区二区三区久久| 青春草国产成人精品久久| 亚洲AV成人精品一区二区三区| 亚洲精品天堂在线观看| 精品国产一二三产品价格| 91久久亚洲国产成人精品性色| 精品人人妻人人澡人人爽人人 | 亚洲熟妇成人精品一区| 国产成人精品亚洲2020| 69久久夜色精品国产69| 久久久这里有精品| 国产精品久久久久久久久久影院| 精品香蕉一区二区三区| 亚洲精品乱码久久久久久蜜桃图片| 国产精品99久久精品| 精品女同一区二区三区免费站| 国产精品成人观看视频免费| 精品国产午夜理论片不卡| 国产第一福利精品导航| 2021久久精品免费观看| 亚洲欧美日韩久久精品| 精品无码人妻一区二区三区18| 国产精品第20页| 国产精品高清久久久久久久| 国产精品久久久久影院| 国产91精品久久久久999| 国产日韩一区二区三免费高清 | 亚洲AV无码精品色午夜果冻不卡 | 2021在线观看视频精品免费| 久久精品国产亚洲av麻豆蜜芽| 国产精品视频第一页| 久久国产综合精品SWAG蓝导航| 国产精品日韩欧美一区二区三区| 日韩精品无码成人专区|