十年專(zhuān)注于品牌網(wǎng)站建設(shè) 十余年專(zhuān)注于網(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ù)熱線(xiàn):400-680-9298,0791-88117053
      掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
      掃一掃打開(kāi)百恒網(wǎng)絡(luò)微信小程序

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

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

      使用IOS蘋(píng)果地圖添加標(biāo)注之觸發(fā)添加動(dòng)作

      百恒網(wǎng)絡(luò) 2017-09-18 7250

      如果要實(shí)現(xiàn)在地圖視圖上添加標(biāo)注點(diǎn),需要兩個(gè)步驟才能實(shí)現(xiàn),第一步是觸發(fā)添加動(dòng)作,第二步是實(shí)現(xiàn)地圖委托方法 mapView:viewForAnnotation:。今天南昌APP制作開(kāi)發(fā)公司-百恒網(wǎng)絡(luò)小編先為大家介紹第一步:使用IOS蘋(píng)果地圖添加標(biāo)注之觸發(fā)添加動(dòng)作,具體操作如下:

      我們通過(guò)“查詢(xún)”按鈕觸發(fā)添加標(biāo)注動(dòng)作,相關(guān)代碼如下:

      @IBAction func geocodeQuery(sender: AnyObject) {

      if (self.txtQueryKey.text == nil) {

      return

      }

      var geocoder = CLGeocoder()

      geocoder.geocodeAddressString(self.txtQueryKey.text,

      completionHandler: { (placemarks, error) -> Void in

      if placemarks.count > 0 {

      self.mapView.removeAnnotations(self.mapView.annotations) ①

      }

      for item in placemarks {

      NSLog("查詢(xún)記錄數(shù):%i", placemarks.count)

      let placemark = item as CLPlacemark

      //調(diào)整地圖位置和縮放比例

      let viewRegion = MKCoordinateRegionMakeWithDistance(

      placemark.location.coordinate, 10000, 10000) ②

      self.mapView.setRegion(viewRegion, animated: true) ③

      let annotation = MyAnnotation(coordinate: placemark.location.coordinate) ④

      annotation.city = placemark.locality

      annotation.state = placemark.administrativeArea

      annotation.streetAddress = placemark.thoroughfare

      annotation.zip = placemark.postalCode ⑤

      self.mapView.addAnnotation(annotation) ⑥

      }

      //關(guān)閉鍵盤(pán)

      self.txtQueryKey.resignFirstResponder()

      })

      }

      - (IBAction)geocodeQuery:(id)sender {

      if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {

      return;

      }

      CLGeocoder *geocoder = [[CLGeocoder alloc] init];

      [geocoder geocodeAddressString:_txtQueryKey.text

      completionHandler:^(NSArray *placemarks, NSError *error) {

      NSLog(@"查詢(xún)記錄數(shù):%lu",[placemarks count]);

      if ([placemarks count] > 0) {

      [self.mapView removeAnnotations:self.mapView.annotations]; ①

      }

      for (int i = 0; i < [placemarks count]; i++) {

      CLPlacemark* placemark = placemarks[i];

      //調(diào)整地圖位置和縮放比例

      MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(

      placemark.location.coordinate, 10000, 10000); ②

      [self.mapView setRegion:viewRegion animated:YES]; ③

      MyAnnotation *annotation = [[MyAnnotation alloc] init]; ④

      annotation.streetAddress = placemark.thoroughfare;

      annotation.city = placemark.locality;

      annotation.state = placemark.administrativeArea;

      annotation.zip = placemark.postalCode;

      annotation.coordinate = placemark.location.coordinate; ⑤

      [self.mapView addAnnotation:annotation]; ⑥

      }

      //關(guān)閉鍵盤(pán)

      [_txtQueryKey resignFirstResponder];

      }];

      }

      當(dāng)用戶(hù)點(diǎn)擊“查詢(xún)”按鈕時(shí),開(kāi)始進(jìn)行地理信息編碼,如果編碼成功,則調(diào)用completionHandler方法。第①行代碼用于移除目前地圖上所有的標(biāo)注點(diǎn),否則反復(fù)點(diǎn)擊“查詢(xún)”按鈕,你會(huì)發(fā)現(xiàn)地圖上的標(biāo)注點(diǎn)越來(lái)越多。

      第②行代碼使用MKCoordinateRegionMakeWithDistance函數(shù)創(chuàng)建一個(gè)結(jié)構(gòu)體MKCoordinateRegion,該結(jié)構(gòu)體封裝了一個(gè)地圖區(qū)域,其定義如下:

      struct MKCoordinateRegion {

      var center: CLLocationCoordinate2D //中心點(diǎn)

      var span: MKCoordinateSpan //跨度

      }

      typedef struct {

      CLLocationCoordinate2D center; //中心點(diǎn)

      MKCoordinateSpan span; //跨度

      } MKCoordinateRegion;

      在上述代碼中,成員center定義了區(qū)域中心點(diǎn),它是CLLocationCoordinate2D結(jié)構(gòu)體類(lèi)型。span成員定義了區(qū)域的跨度,它是MKCoordinateSpan結(jié)構(gòu)體類(lèi)型。MKCoordinateSpan結(jié)構(gòu)體封裝了在地圖上的跨度信息,它的定 義如下:

      struct MKCoordinateSpan {

      var latitudeDelta: CLLocationDegrees //區(qū)域的南北跨度

      var longitudeDelta: CLLocationDegrees //區(qū)域的東西跨度

      }

      typedef struct {

      CLLocationDegrees latitudeDelta; //區(qū)域的南北跨度

      CLLocationDegrees longitudeDelta; //區(qū)域的東西跨度

      } MKCoordinateSpan;

      在上述代碼中,latitudeDelta為南北跨度,它的單位是“度”,1度大約是111公里。longitudeDelta為東西 跨度,在赤道上1度大約是111公里,隨著靠近兩極,這個(gè)距離逐步變小,在地球的兩個(gè)極點(diǎn)時(shí)變?yōu)?公里。它們 是有差別的,這源于我們地球經(jīng)線(xiàn)和緯線(xiàn)的中心點(diǎn)不同。

      在第②行代碼中,MKCoordinateRegionMakeWithDistance函數(shù)的第一個(gè)參數(shù)是CLLocationCoordinate2D結(jié)構(gòu), 指定了目標(biāo)區(qū)域的中心點(diǎn);第二個(gè)參數(shù)是目標(biāo)區(qū)域南北的跨度,其單位是米;第三個(gè)參數(shù)是目標(biāo)區(qū)域東西的跨度,其單位是米。后面兩個(gè)參數(shù)的調(diào)整會(huì)影響地圖縮放。

      第③行代碼重新設(shè)置地圖視圖的顯示區(qū)域。animated設(shè)置為true(或YES)時(shí),會(huì)使地圖有“飛”過(guò)去的動(dòng) 畫(huà)效果。

      第④行代碼用于實(shí)例化MyAnnotation對(duì)象。MyAnnotation類(lèi)是我們自定義的實(shí)現(xiàn)MKAnnotation協(xié)議的地圖標(biāo)注 點(diǎn)類(lèi)。因?yàn)榈貓D上的標(biāo)注點(diǎn)是MKPinAnnotationView(大頭針標(biāo)注視圖)類(lèi)型,這個(gè)視圖要求標(biāo)注點(diǎn)信息由實(shí)現(xiàn) MKAnnotation協(xié)議的類(lèi)提供。如果標(biāo)識(shí)點(diǎn)上顯示的信息是固定的,可以使用Map Kit API實(shí)現(xiàn)MKPointAnnotation 標(biāo)注類(lèi)。第④~⑤行代碼將地標(biāo)CLPlacemark對(duì)象信息取出,放入到MapLocation對(duì)象中。為什么要這樣導(dǎo)來(lái)導(dǎo)去呢? 這是因?yàn)樵贛KPinAnnotationView視圖中,只能接收實(shí)現(xiàn)MKAnnotation協(xié)議的類(lèi),而地標(biāo)類(lèi)CLPlacemark沒(méi)有實(shí)現(xiàn) MKAnnotation協(xié)議。

      第⑥行代碼把標(biāo)注點(diǎn)MyAnnotation對(duì)象添加到地圖視圖上。一旦該方法被調(diào)用,地圖視圖委托方法mapView: viewForAnnotation:就會(huì)被回調(diào)。

      以上就是百恒網(wǎng)絡(luò)為大家介紹的關(guān)于在南昌APP開(kāi)發(fā)中使用IOS蘋(píng)果地圖添加標(biāo)注時(shí)觸發(fā)添加動(dòng)作的方法,大家可以先了解一下,后面本公司還會(huì)為大家介紹添加標(biāo)注的第二步,那就是實(shí)現(xiàn)地圖委托方法 mapView:viewForAnnotation:,希望對(duì)大家有所幫助!

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

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

      售前咨詢(xún) 售前咨詢(xún)
       
      售前咨詢(xún) 售前咨詢(xún)
       
      售前咨詢(xún) 售前咨詢(xún)
       
      售前咨詢(xún) 售前咨詢(xún)
       
      售前咨詢(xún) 售前咨詢(xún)
       
      售后服務(wù) 售后服務(wù)
       
      售后服務(wù) 售后服務(wù)
       
      備案專(zhuān)線(xiàn) 備案專(zhuān)線(xiàn)
       
      ×
      国产精品ⅴ无码大片在线看| 亚洲人成在线精品| 日韩精品一区二区三区影院| 国产精品青草久久久久婷婷| 99ri精品国产亚洲| 久久久无码精品国产一区| 中文字幕一精品亚洲无线一区| 亚洲国产精品专区在线观看| 亚洲av无码日韩av无码网站冲| 国产AV一区二区精品凹凸| 亚洲日韩精品A∨片无码加勒比| 亚洲乱人伦精品图片| 久久精品国产第一区二区三区 | 中文字幕在线精品视频入口一区| 2048亚洲精品国产| 黑猫福利精品第一视频| 中文字幕日韩人妻不卡一区| 国产精品免费视频网站| 日本精品3d动漫一区二区| 香蕉久久夜色精品国产2020| 久热爱精品视频线路一| 99热在线精品免费播放6| 日产精品久久久久久久| 人妻精品久久久久中文字幕一冢本| 久久国产精品久久国产精品| 久久久精品一区二区三区| 国产精品免费无遮挡无码永久视频| 亚洲精品无码永久在线观看你懂的| 久久国产美女免费观看精品| 久久午夜无码鲁丝片午夜精品| CHINESE中国精品自拍| 老司机成人精品视频lsj| 伊人精品视频一区二区三区| 综合国产精品第一页| 99精品国产99久久久久久97| 538prom精品视频我们不只是| 华人在线精品免费观看| 久久久久无码精品亚洲日韩| 99re热精品这里精品| 少妇伦子伦精品无吗| 亚洲日韩精品射精日|