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

      百恒網絡

      南昌百恒網絡

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

      百恒網絡 2017-09-19 5801

      昨天南昌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
      掃一掃關注百恒網絡微信公眾號
      掃一掃打開百恒網絡小程序

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

      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售后服務 售后服務
       
      售后服務 售后服務
       
      備案專線 備案專線
       
      ×
      囯产精品久久久久久久久久妞妞| 国产香蕉精品视频在| 国内精品一区二区三区最新| 亚洲国产精品成人久久久 | 亚洲精品中文字幕乱码| 久久精品国产精品亚洲艾草网| 最新国产在线精品观看| 2021国产成人午夜精品| 精品一区二区三区四区| 国产精品视频色视频| 日韩高清在线观看| 日韩精品无码一区二区中文字幕| 国产精品高清m3u8在线播放 | 日韩AV影院在线观看| 精品国产日韩亚洲一区91| 亚洲精品欧美综合四区| 国产伦精品一区二区三区女| 亚洲国产成人91精品| 91探花国产综合在线精品| 亚洲日韩国产精品无码av| 久久精品国产亚洲AV大全| 日韩精品真人荷官无码| 亚洲综合国产精品| 乱码精品一区二区三区| 久久夜色精品国产噜噜噜亚洲AV| 午夜精品美女写真福利| 乱色精品无码一区二区国产盗 | 亚洲乱码日产精品一二三| 国产精品1024永久免费视频| 国产精品毛片久久久久久久| 精品综合久久久久久98| 国产精品蜜芽在线观看| 国产成人精品亚洲2020| 思思re热免费精品视频66| 无码国产精品久久一区免费| 久久这里只有精品国产免费10| 日韩精品一线二线三线优势| 国产精品视频免费一区二区| 日韩精品一区二区三区老鸦窝| 精品国产杨幂在线观看| 国产日韩精品视频|