国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發中使用CoreLocation框架處理地理編碼的方法

2020-07-26 03:30:36
字體:
來源:轉載
供稿:網友

一、簡介

1.在移動互聯網時代,移動app能解決用戶的很多生活瑣事,比如

(1)導航:去任意陌生的地方

(2)周邊:找餐館、找酒店、找銀行、找電影院

2.在上述應用中,都用到了地圖和定位功能,在iOS開發中,要想加入這2大功能,必須基于2個框架進行開發

(1)Map Kit :用于地圖展示

(2)Core Location :用于地理定位 

3.兩個熱門專業術語

(1)LBS :Location Based Service(基于定位的服務)

(2)SoLoMo :Social Local Mobile(索羅門)

二、CoreLocation框架的使用

1.CoreLocation框架使用前提

(1)導入框架

20151225101427747.png (390×158)

說明:在Xcode5以后,不再需要我們手動導入

(2)導入主頭文件

  

復制代碼 代碼如下:
#import <CoreLocation/CoreLocation.h>

2.CoreLocation框架使用須知

CoreLocation框架中所有數據類型的前綴都是CL

CoreLocation中使用CLLocationManager對象來做用戶定位

 

三、經緯度等地理信息掃盲

1.示意圖

20151225101444971.png (426×346)

2.本初子午線:穿過英國倫敦格林文治天文臺

往東邊(右邊)走,是東經(E)

往西邊(左邊)走,是西經(W)

東西經各180°,總共360°

3.赤道:零度維度

往北邊(上邊)走,是北緯(N)

往南邊(下邊)走,是南緯(S)

南北緯各90°,總共180°

提示:橫跨經度/緯度越大(1° ≈ 111km),表示的范圍就越大,在地圖上看到的東西就越小

4.我國的經緯度:

(1)中國的經緯度范圍

緯度范圍:N 3°51′ ~  N 53°33′

經度范圍:E 73°33′ ~  E 135°05′

(2)部分城市的經緯度

20151225101510968.png (637×232)

四、模擬位置

說明:在對程序進行測試的時候,設置手機模擬器的模擬位置(經緯度)

20151225101650044.png (438×306)

20151225101708140.png (392×208)

CoreLocation地理編碼
一、簡單說明

CLGeocoder:地理編碼器,其中Geo是地理的英文單詞Geography的簡寫。

1.使用CLGeocoder可以完成“地理編碼”和“反地理編碼”

地理編碼:根據給定的地名,獲得具體的位置信息(比如經緯度、地址的全稱等)

反地理編碼:根據給定的經緯度,獲得具體的位置信息

(1)地理編碼方法

復制代碼 代碼如下:

  - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

(2)反地理編碼方法
復制代碼 代碼如下:

  - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

 
2.CLGeocodeCompletionHandler

  當地理/反地理編碼完成時,就會調用CLGeocodeCompletionHandler

20151225101730528.png (637×133)

這個block傳遞2個參數

error :當編碼出錯時(比如編碼不出具體的信息)有值

placemarks :里面裝著CLPlacemark對象

3.CLPlacemark

說明:CLPlacemark的字面意思是地標,封裝詳細的地址位置信息

地理位置     @property (nonatomic, readonly) CLLocation *location;  

區域       @property (nonatomic, readonly) CLRegion *region;

詳細的地址信息   @property (nonatomic, readonly) NSDictionary *addressDictionary;

地址名稱    @property (nonatomic, readonly) NSString *name;

城市      @property (nonatomic, readonly) NSString *locality;

二、代碼示例:

在storyboard中搭建界面如下:

20151225101747444.png (316×498)

實現代碼:

復制代碼 代碼如下:

  YYViewController.m文件
//
//  YYViewController.m
//  19-地理編碼
//
//  Created by apple on 14-8-11.
//  Copyright (c) 2014年 yangyong. All rights reserved.
//

#import "YYViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface YYViewController ()
@property(nonatomic,strong)CLGeocoder *geocoder;
#pragma mark-地理編碼
- (IBAction)geocode;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;

#pragma mark-反地理編碼

- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverdeDetailAddressLabel;
@end


復制代碼 代碼如下:

@implementation YYViewController

#pragma mark-懶加載
-(CLGeocoder *)geocoder
{
    if (_geocoder==nil) {
        _geocoder=[[CLGeocoder alloc]init];
    }
    return _geocoder;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
/**
 *  地理編碼:地名―>經緯度坐標
 */
- (IBAction)geocode {
    //1.獲得輸入的地址
    NSString *address=self.addressField.text;
    if (address.length==0) return;
   
    //2.開始地理編碼
    //說明:調用下面的方法開始編碼,不管編碼是成功還是失敗都會調用block中的方法
    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        //如果有錯誤信息,或者是數組中獲取的地名元素數量為0,那么說明沒有找到
        if (error || placemarks.count==0) {
            self.detailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";
        }else   //  編碼成功,找到了具體的位置信息
        {
            //打印查看找到的所有的位置信息
                /*
                    name:名稱
                    locality:城市
                    country:國家
                    postalCode:郵政編碼
                 */
            for (CLPlacemark *placemark in placemarks) {
                NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);
            }
           
            //取出獲取的地理信息數組中的第一個顯示在界面上
            CLPlacemark *firstPlacemark=[placemarks firstObject];
            //詳細地址名稱
            self.detailAddressLabel.text=firstPlacemark.name;
            //緯度
            CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
            //經度
            CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
            self.latitudeLabel.text=[NSString stringWithFormat:@"%.2f",latitude];
            self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",longitude];
        }
    }];
}

/**
 *  反地理編碼:經緯度坐標―>地名
 */
- (IBAction)reverseGeocode {
    //1.獲得輸入的經緯度
    NSString *longtitudeText=self.longitudeField.text;
    NSString *latitudeText=self.latitudeField.text;
    if (longtitudeText.length==0||latitudeText.length==0) return;
   
    CLLocationDegrees latitude=[latitudeText doubleValue];
    CLLocationDegrees longitude=[longtitudeText doubleValue];
   
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    //2.反地理編碼
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error||placemarks.count==0) {
            self.reverdeDetailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";
        }else//編碼成功
        {
            //顯示最前面的地標信息
            CLPlacemark *firstPlacemark=[placemarks firstObject];
            self.reverdeDetailAddressLabel.text=firstPlacemark.name;
            //經緯度
            CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
            CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
            self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];
            self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];
        }
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end


實現效果:

(1)地理編碼:(地名->經緯度坐標)

20151225101817256.png (315×496)

打印輸出:

20151225101928782.png (841×85)

(2)反地理編碼:(經緯度―>地名)

20151225101945462.png (314×498)

(3)注意:調整鍵盤

20151225102000450.png (715×229)

點擊經緯度textField進行輸入的時候,彈出的鍵盤如下

20151225102021300.png (317×495)

(4)注意:搜索的所有結果都是在中國境內的,因為蘋果在中國的地圖服務商是高德地圖。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆元县| 惠安县| 怀仁县| 哈巴河县| 阿图什市| 罗甸县| 治县。| 辽中县| 龙里县| 新蔡县| 广西| 招远市| 鹤壁市| 中卫市| 杭锦旗| 广丰县| 滕州市| 文登市| 株洲县| 大关县| 濮阳市| 定边县| 宜昌市| 伊金霍洛旗| 唐河县| 昌乐县| 会同县| 南开区| 陆川县| 贵南县| 峡江县| 贵德县| 抚顺市| 都兰县| 衡水市| 南川市| 翼城县| 井陉县| 会宁县| 沙雅县| 普安县|