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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:56:31
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS開發中使用CoreLocation框架處理地理編碼的方法,代碼基于傳統的Objective-C,需要的朋友可以參考下
 

一、簡介

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)導入框架

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

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

(2)導入主頭文件

  

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

 

2.CoreLocation框架使用須知

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

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

 

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

1.示意圖

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

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)部分城市的經緯度

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

四、模擬位置

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

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

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

 

 

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

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

這個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中搭建界面如下:

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

實現代碼:

復制代碼代碼如下:

  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)地理編碼:(地名->經緯度坐標)

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

打印輸出:

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

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

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

(3)注意:調整鍵盤

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

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

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

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



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漳平市| 色达县| 汽车| 观塘区| 垫江县| 海南省| 察雅县| 桓台县| 双辽市| 札达县| 黄骅市| 大城县| 新建县| 偃师市| 洛浦县| 图木舒克市| 永康市| 鄂托克前旗| 靖西县| 商水县| 永安市| 鄂伦春自治旗| 宁安市| 浙江省| 邵东县| 武穴市| 抚松县| 永州市| 辰溪县| 宁远县| 九龙坡区| 太仆寺旗| 桃江县| 庆元县| 苍溪县| 翁牛特旗| 伽师县| 苏尼特左旗| 张家川| 社旗县| 镇巴县|