美文网首页
iOS定位(LBS)

iOS定位(LBS)

作者: 丶若若 | 来源:发表于2016-12-23 20:41 被阅读216次

CoreLocation 地理定位

1.创建定位管理者

CLLocationManager *mgr = [[CLLocationManager  alloc]init];

2.设置代理

mgr =  self;

3.开始定位

[mgr startUpdatingLocation];

4.计算两个地理位置之间的距离

CLLocation *location1 = [[CLLocation alloc]initWithLatitude:20.20 longitude:111.230];

CLLocation *location2 = [[CLLocation alloc]initWithLatitude:66.20 longitude:222.230];

CLLocationDistance distance = [location1 distanceFormLocation:location2];

NSLog(@"两个地理位置之间的距离%f",distance);

定位的代理方法

-(void)locationmanager:(CLLocationmanager*)manager didupdateLocations:(NSArray *)locations{

NSLog(@"定位到用户的位置,如果不停止会一直定位");

//获取用户位置的对象

CLLocation *location = [locations lastObject];

CLLocationCoordinate2D coordinate = location.coordinate;

NSLog(@"纬度:%f  经度:%f",coordinate.latitude,coordinate.longitude);

[manager stopUpdatingLocation];

// 1.取出用户输入的地址

NSString *address = self.addressFIeld.text;

if (address.length == 0) {

NSLog(@"输入地址不能为空");

return;

}

地理编码geocode

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

[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

// 1.如果解析有错误,或者解析出的数组个数为0.直接返回

if (placemarks.count == 0 || error) return;

// 2.便利所有的地标对象(如果是实际开发,可以给用户以列表的形式展示)

for (CLPlacemark *pm in placemarks) {

// 2.1.取出用户的位置信息

CLLocation *location = pm.location;

// 2.2.取出用户的经纬度

CLLocationCoordinate2D coordinate = location.coordinate;

// 2.3.将信息设置到界面上

self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", coordinate.latitude];

self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f",coordinate.longitude];

self.resultLabel.text = pm.name;

}

}];

反地理编码rervsecode

// 1.取出输入的经纬度

NSString *latitude = self.latitudeField.text;

NSString *longitude = self.longitudeField.text;

if (latitude.length == 0 || longitude.length == 0) {

return;

}

// 2.反地理编码

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

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.floatValue longitude:longitude.floatValue];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

// 如果有错误,或者解析出来的地址数量为0

if (placemarks.count == 0 || error) return ;

// 取出地标,就可以取出地址信息,以及CLLocation对象

CLPlacemark *pm = [placemarks firstObject];

#warning 注意:如果是取出城市的话,需要判断locality属性是否有值(直辖市时,该属性为空)

if (pm.locality) {

self.resultLabel.text = pm.locality;

} else {

self.resultLabel.text = pm.administrativeArea;

}

}];

相关文章

  • iOS定位(LBS)

    CoreLocation 地理定位 1.创建定位管理者 CLLocationManager *mgr = [[CL...

  • 2016-08-25 LBS定位服务接口

    LBS(基于位置服务) LBS定义 LBS定位不足: GPS定义 GPS定位优点: LBS基站定位和GPS定位的区...

  • iOS笔记-定位(LBS)小结

    iOS8.0之前的定位 前台定位 导入CoreLocation框架和对应的主头文件#import

  • iOS定位服务设计实例一则

    iOS定位服务设计实例一则 当前,越来越多的移动应用基于LBS(位置服务)构建业务,LBS可以说是移动应用浪潮的基...

  • golang 通过 Redis GEO 实现 LBS 功能

    LBS 基于位置的服务(Location Based Services,LBS),是利用各类型的定位技术来获取定位...

  • LBS 定位开发

    一、GPS和LBS GPS卫星定位系统 LBS根据IP地址定位,国内目前有百度、腾讯、高德LBS,国际上有谷歌LB...

  • LBS 定位开发

    一、GPS和LBS 1.GPS卫星定位系统 2.LBS根据IP地址定位,国内目前有百度、腾讯、高德LBS,国际上有...

  • LBS

    什么是LBS LBS全称Location Based Service,即基于位置的服务。如果非要说LBS定位总觉得...

  • GPS定位器中的“LBS”是什么?

    很多使用GPS定位器的用户都会在设备的说明书或者产品的宣传上看到定位器支持LBS定位,但LBS定位是什么意思呢,可...

  • LBS地图定位

    定位使用 " CoreLocation 框架想要定位,需要使用5个步骤 1.首先创建一个"强引用"的位置管理器C...

网友评论

      本文标题:iOS定位(LBS)

      本文链接:https://www.haomeiwen.com/subject/etuodttx.html