iOS开发系列--地图与定位源代码详解

2020-01-18 17:38:32王振洲

注意:

1.定位频率和定位精度并不应当越精确越好,需要视实际情况而定,因为越精确越耗性能,也就越费电。

2.定位成功后会根据设置情况频繁调用-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations方法,这个方法返回一组地理位置对象数组,每个元素一个CLLocation代表地理位置信息(包含经度、纬度、海报、行走速度等信息),之所以返回数组是因为有些时候一个位置点可能包含多个位置。

3.使用完定位服务后如果不需要实时监控应该立即关闭定位服务以节省资源。

4.除了提供定位功能,CLLocationManager还可以调用startMonitoringForRegion:方法对指定区域进行监控。

地理编码

除了提供位置跟踪功能之外,在定位服务中还包含CLGeocoder类用于处理地理编码和逆地理编码(又叫反地理编码)功能。

地理编码:根据给定的位置(通常是地名)确定地理坐标(经、纬度)。

逆地理编码:可以根据地理坐标(经、纬度)确定位置信息(街道、门牌等)。

CLGeocoder最主要的两个方法就是- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;和- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;,分别用于地理编码和逆地理编码。下面简单演示一下:


//
// KCMainViewController.m
// CoreLocation
//
// Created by Kenshin Cui on 14-03-27.
// Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

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

@interface KCMainViewController ()<CLLocationManagerDelegate>{

  CLGeocoder *_geocoder;
}

@end

@implementation KCMainViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  _geocoder=[[CLGeocoder alloc]init];
  [self getCoordinateByAddress:@"北京"];
  [self getAddressByLatitude:39.54 longitude:116.28];
}

#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address{
  //地理编码
  [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
    //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
    CLPlacemark *placemark=[placemarks firstObject];
    
    CLLocation *location=placemark.location;//位置
    CLRegion *region=placemark.region;//区域
    NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
//    NSString *name=placemark.name;//地名
//    NSString *thoroughfare=placemark.thoroughfare;//街道
//    NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
//    NSString *locality=placemark.locality; // 城市
//    NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
//    NSString *administrativeArea=placemark.administrativeArea; // 州
//    NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
//    NSString *postalCode=placemark.postalCode; //邮编
//    NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
//    NSString *country=placemark.country; //国家
//    NSString *inlandWater=placemark.inlandWater; //水源、湖泊
//    NSString *ocean=placemark.ocean; // 海洋
//    NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
    NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);
  }];
}

#pragma mark 根据坐标取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
  //反地理编码
  CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
  [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    CLPlacemark *placemark=[placemarks firstObject];
    NSLog(@"详细信息:%@",placemark.addressDictionary);
  }];
}

@end