十年專注于品牌網(wǎng)站建設(shè) 十余年專注于網(wǎng)站建設(shè)_小程序開(kāi)發(fā)_APP開(kāi)發(fā),低調(diào)、敢創(chuàng)新、有情懷!
      南昌百恒網(wǎng)絡(luò)微信公眾號(hào) 掃一掃關(guān)注
      小程序
      tel-icon全國(guó)服務(wù)熱線:400-680-9298,0791-88117053
      掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
      掃一掃打開(kāi)百恒網(wǎng)絡(luò)微信小程序

      百恒網(wǎng)絡(luò)

      南昌百恒網(wǎng)絡(luò)

      實(shí)現(xiàn)地圖委托方法mapView:viewForAnnotation:

      百恒網(wǎng)絡(luò) 2017-09-19 5700

      昨天南昌APP制作開(kāi)發(fā)公司-百恒網(wǎng)絡(luò)為大家介紹了使用IOS蘋(píng)果地圖添加標(biāo)注的第一個(gè)步驟:觸發(fā)添加動(dòng)作,那么今天本公司就來(lái)為大家介紹第二步:實(shí)現(xiàn)地圖委托方法 mapView:viewForAnnotation:,這也是最后一步!具體方法如下:

      MKMapViewDelegate委托協(xié)議方法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:在地圖視圖添加標(biāo)注時(shí)回調(diào)。給地圖視圖添加標(biāo)注的方法是self.mapView.addAnnotation(annotation),其中annotation是地圖標(biāo)注對(duì)象。

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

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

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

      canShowCallout設(shè)置為true的情況

      圖1 canShowCallout設(shè)置為true的情況

      在委托方法的后,返回annotationView標(biāo)注點(diǎn)視圖對(duì)象。

      最后,我們看看自定義標(biāo)注類MyAnnotation。MyAnnotation的定義如下:

      import MapKit

      class MyAnnotation: NSObject, MKAnnotation{

      //街道信息屬性

      var streetAddress: String!

      //城市信息屬性

      var city: String!

      //州、省、市信息

      var state: String!

      //郵編

      var zip: String!

      //地理坐標(biāo)

      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;

      //地理坐標(biāo)

      @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

      地圖上的標(biāo)注點(diǎn)類必須實(shí)現(xiàn)MKAnnotation協(xié)議。MKAnnotation協(xié)議需要重寫(xiě)如下兩個(gè)屬性。 ?

      1、title:標(biāo)注點(diǎn)上的主標(biāo)題。

      2、subtitle:標(biāo)注點(diǎn)上的副標(biāo)題。

      在重寫(xiě)subtitle屬性時(shí),我們將它的相關(guān)信息拼接成字符串賦值給它。這里,我們可以根據(jù)自己的需要和習(xí)慣拼接在這個(gè)字符串的前后。

      好了,關(guān)于在南昌APP開(kāi)發(fā)中實(shí)現(xiàn)在地圖視圖上添加標(biāo)注點(diǎn)的方法就已經(jīng)介紹完了,如果大家對(duì)于第一個(gè)步驟:《使用IOS蘋(píng)果地圖添加標(biāo)注之觸發(fā)添加動(dòng)作》的方法還不太理解的話,可點(diǎn)擊再重新看一遍,或者來(lái)電咨詢百恒網(wǎng)絡(luò),我們專業(yè)為您解答!


      400-680-9298,0791-88117053
      掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
      掃一掃打開(kāi)百恒網(wǎng)絡(luò)小程序

      歡迎您的光顧,我們將竭誠(chéng)為您服務(wù)×

      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
      国产精品成人啪精品视频免费| 99精品高清视频一区二区| www.99精品| 久久夜色精品国产网站| 日韩免费无码视频一区二区三区| 国内精品久久久久影院网站| 日韩美女va毛片在线播放| 久久99精品久久久久久hb无码 | 正在播放国产精品放孕妇| 国产91精品一区| 久久国产精品久久久| 中日韩精品电影推荐网站| 久久亚洲私人国产精品vA | 老司机性色福利精品视频| 精品国产福利久久久| 99re6在线视频精品免费| 黑人无码精品又粗又大又长| 国产亚洲精品成人AA片| 国产日韩精品一区二区在线观看播放| 91麻豆精品激情在线观看最新| 国产精品超碰12396| 日韩精品在线一区二区| 国产成人无码精品久久久露脸 | 在线精品视频播放| 九九热在线精品视频| 亚洲精品卡2卡3卡4卡5卡区| 麻豆精品国产免费观看 | 亚洲av永久无码精品秋霞电影秋 | 99j久久精品久久久久久| 日韩在线一区视频| 久久精品国产亚洲av日韩 | 久久亚洲精品国产精品黑人| 久这里只精品99re66| 国产精品国产三级国产专播| 国产精品无码无在线观看| 亚洲欧美日韩中文字幕在线一区| 91在线手机精品免费观看| 久久精品一区二区免费看| 一本久久A久久免费精品不卡| 精品一区二区三区无码免费直播| 久久66热人妻偷产精品9|