欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发中使用CoreLocation框架处理地理编码的方法

程序员文章站 2023-10-29 12:00:10
一、简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如 (1)导航:去任意陌生的地方 (2)周边:找餐馆、找酒店、找银行、找电影院 2.在上述应用...

一、简介

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)注意:搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图。