1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      iOS開發(fā):通過經(jīng)緯度獲得城市、省份等信息-創(chuàng)新互聯(lián)

          iOS系統(tǒng)自帶定位,用CLLocationManager就可以輕松的實(shí)現(xiàn)定位的操作,獲得的是一組經(jīng)緯度,當(dāng)然,也可以根據(jù)給出的經(jīng)緯度獲取相應(yīng)的省份、城市、街道等信息,下面就看一個(gè)根據(jù)經(jīng)緯度獲得城市的demo:

      創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比西崗網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式西崗網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋西崗地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

              因?yàn)楂@取經(jīng)緯度需要CLLocationManager類,而這個(gè)類包含在CoreLocation框架中,獲取城市信息需要mapKit框架,所以需要首先在工程中導(dǎo)入這兩個(gè)框架:

      導(dǎo)入框架的步驟:選擇1.target——2.Build Phases——3.Link Binary With Libraries ——4.點(diǎn)擊“+”號(hào):如圖所示步驟:
      iOS開發(fā):通過經(jīng)緯度獲得城市、省份等信息

      點(diǎn)擊加號(hào)之后在搜索框里輸入相應(yīng)的框架,即可搜索到,如圖所示:

      iOS開發(fā):通過經(jīng)緯度獲得城市、省份等信息

      iOS開發(fā):通過經(jīng)緯度獲得城市、省份等信息

      下面就該寫代碼了,首先在視圖控制器中導(dǎo)入:

      #import 
      #import 

      兩個(gè)頭文件,然后.m中的具體代碼如下:

      #import "ANNViewController.h"
      
      
      @interface ANNViewController ()
      @property (strong, nonatomic) IBOutlet UILabel *longitude;
      @property (strong, nonatomic) IBOutlet UILabel *latitude;
      
      @property (strong, nonatomic) IBOutlet UILabel *location;
      @property (strong, nonatomic) CLLocationManager *locationManager;
      
      @end
      
      @implementation ANNViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
      	// Do any additional setup after loading the view, typically from a nib.
          self.view.backgroundColor = [UIColor whiteColor];
          
          //創(chuàng)建CLLocationManager對(duì)象
          self.locationManager = [[CLLocationManager alloc] init];
          //設(shè)置代理為自己
          self.locationManager.delegate = self;
          
      }
      - (IBAction)locationButton:(UIButton *)sender {
          [self.locationManager startUpdatingLocation];
      }
      
      - (void)locationManager:(CLLocationManager *)manager
      	didUpdateToLocation:(CLLocation *)newLocation
      		   fromLocation:(CLLocation *)oldLocation
      {
          
          //將經(jīng)度顯示到label上
          self.longitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.longitude];
          //將緯度現(xiàn)實(shí)到label上
          self.latitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.latitude];
          
          // 獲取當(dāng)前所在的城市名
          CLGeocoder *geocoder = [[CLGeocoder alloc] init];
          //根據(jù)經(jīng)緯度反向地理編譯出地址信息
          [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
           {
               if (array.count > 0)
               {
                   CLPlacemark *placemark = [array objectAtIndex:0];
                   
                   //將獲得的所有信息顯示到label上
                   self.location.text = placemark.name;
                   //獲取城市
                   NSString *city = placemark.locality;
                   if (!city) {
                       //四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)
                       city = placemark.administrativeArea;
                   }
                   NSLog(@"city = %@", city);
                   
               }
               else if (error == nil && [array count] == 0)
               {
                   NSLog(@"No results were returned.");
               }
               else if (error != nil)
               {
                   NSLog(@"An error occurred = %@", error);
               }
           }];
          
          //系統(tǒng)會(huì)一直更新數(shù)據(jù),直到選擇停止更新,因?yàn)槲覀冎恍枰@得一次經(jīng)緯度即可,所以獲取之后就停止更新
          [manager stopUpdatingLocation];
      }

      主要就是直轄市的城市獲得需要拐個(gè)彎,iOS7添加了一個(gè)新的方法,代替了上面這個(gè)方法:

      - (void)locationManager:(CLLocationManager *)manager
      	 didUpdateLocations:(NSArray *)locations
      {
          NSLog(@"longitude = %f", ((CLLocation *)[locations
      lastObject]).coordinate.longitude);
          NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude);
          
          [manager stopUpdatingLocation];
      }

      后面的處理和上面的方法一樣,大家可以看一下。

      另外還有一些CLGeocoder的屬性如下:

      @property (nonatomic, readonly) NSDictionary *addressDictionary;
      
      // address dictionary properties
      @property (nonatomic, readonly) NSString *name; // eg. Apple Inc.
      @property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop
      @property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1
      @property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino
      @property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District
      @property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA
      @property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara
      @property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014
      @property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US
      @property (nonatomic, readonly) NSString *country; // eg. United States
      @property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe
      @property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean
      @property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park

      完整的工程如下:

      附件下載地址:https://github.com/winann/TestLocation.git

      附件:http://down.51cto.com/data/2364690

      另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


      網(wǎng)頁標(biāo)題:iOS開發(fā):通過經(jīng)緯度獲得城市、省份等信息-創(chuàng)新互聯(lián)
      本文網(wǎng)址:http://www.ef60e0e.cn/article/coeieo.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        德安县| 宁晋县| 安国市| 鹤山市| 北流市| 莒南县| 阳山县| 通渭县| 秭归县| 嘉黎县| 凌源市| 和顺县| 永新县| 定南县| 曲周县| 博罗县| 通辽市| 彭阳县| 曲周县| 江陵县| 鸡泽县| 大洼县| 平泉县| 京山县| 梧州市| 广饶县| 大足县| 桃园县| 阿坝| 钟祥市| 龙口市| 玉环县| 高尔夫| 曲水县| 元氏县| 安宁市| 张家界市| 铁岭市| 错那县| 泸溪县| 石河子市|