此文章主要介绍如何在Swift下使用CLLocationManager来获取定位的经纬度,并根据经纬度进行你地理编码来获取定位点的信息。
先导入MapKit头文件
1 |
import MapKit |
为了让代码更容易理解,我预先在StoryBoard创建好了初始界面,并简单的做了下屏幕适配。

在iOS8以上,苹果官方规定,使用定位前都必须要先经过用户同意。
先在Info.plist里添加2个Key,NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription。之前我只添加了一个key,发现一直没有向用户发送允许请求定位的信息。把2个key都加进去就好了。

CLLocationManager
1 2 3 4 5 6 7 8 |
self.locateManage.delegate = self //请求定位权限 if self.locateManage.respondsToSelector(Selector("requestAlwaysAuthorization")) { self.locateManage.requestAlwaysAuthorization() } self.locateManage.desiredAccuracy = kCLLocationAccuracyBest//定位精准度 self.locateManage.startUpdatingLocation()//开始定位 |
MapView上显示定位点
1 |
self.mapViewLoca.showsUserLocation = true |
为了节省用户电量,苹果建议我们,一般情况下,最好不要频繁地获取定位信息,这个教程里做了个示范,我在CLLocationManager定位代理方法里面只要获取到一次信息,就停止定位,并在定位的经纬度保存到属性
1 |
var currentCoordinate:CLLocationCoordinate2D? |
CLLocationManager定位代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("hello") if let newLoca = locations.last { CLGeocoder().reverseGeocodeLocation(newLoca, completionHandler: { (pms, err) -> Void in if let newCoordinate = pms?.last?.location?.coordinate { //此处设置地图中心点为定位点,缩放级别18 self.mapViewLoca.setCenterCoordinateLevel(newCoordinate, zoomLevel: 15, animated: true) manager.stopUpdatingLocation()//停止定位,节省电量,只获取一次定位 self.currentCoordinate = newCoordinate//记录定位点经纬度 //取得最后一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址 let placemark:CLPlacemark = (pms?.last)! let location = placemark.location;//位置 let region = placemark.region;//区域 let addressDic = placemark.addressDictionary;//详细地址信息字典,包含以下部分信息 // let name=placemark.name;//地名 // let thoroughfare=placemark.thoroughfare;//街道 // let subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等 // let locality=placemark.locality; // 城市 // let subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑 // let administrativeArea=placemark.administrativeArea; // 州 // let subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息 // let postalCode=placemark.postalCode; //邮编 // let ISOcountryCode=placemark.ISOcountryCode; //国家编码 // let country=placemark.country; //国家 // let inlandWater=placemark.inlandWater; //水源、湖泊 // let ocean=placemark.ocean; // 海洋 // let areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标 print(location,region,addressDic) } }) } } |
MKMapView
MKMapView提供的方法里并没有设置地图缩放级别的方法。这个教程里我给MKMapView添加了个extension
为了让代码更容易理解,我预先在StoryBoard创建好了初始界面,并简单的做了下屏幕适配。

在iOS8以上,苹果官方规定,使用定位前都必须要先经过用户同意。
先在Info.plist里添加2个Key,NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription。之前我只添加了一个key,发现一直没有向用户发送允许请求定位的信息。把2个key都加进去就好了。

CLLocationManager
1 2 3 4 5 6 7 8 |
self.locateManage.delegate = self //请求定位权限 if self.locateManage.respondsToSelector(Selector("requestAlwaysAuthorization")) { self.locateManage.requestAlwaysAuthorization() } self.locateManage.desiredAccuracy = kCLLocationAccuracyBest//定位精准度 self.locateManage.startUpdatingLocation()//开始定位 |
MapView上显示定位点
1 |
self.mapViewLoca.showsUserLocation = true |
为了节省用户电量,苹果建议我们,一般情况下,最好不要频繁地获取定位信息,这个教程里做了个示范,我在CLLocationManager定位代理方法里面只要获取到一次信息,就停止定位,并在定位的经纬度保存到属性
1 |
var currentCoordinate:CLLocationCoordinate2D? |
CLLocationManager定位代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("hello") if let newLoca = locations.last { CLGeocoder().reverseGeocodeLocation(newLoca, completionHandler: { (pms, err) -> Void in if let newCoordinate = pms?.last?.location?.coordinate { //此处设置地图中心点为定位点,缩放级别18 self.mapViewLoca.setCenterCoordinateLevel(newCoordinate, zoomLevel: 15, animated: true) manager.stopUpdatingLocation()//停止定位,节省电量,只获取一次定位 self.currentCoordinate = newCoordinate//记录定位点经纬度 //取得最后一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址 let placemark:CLPlacemark = (pms?.last)! let location = placemark.location;//位置 let region = placemark.region;//区域 let addressDic = placemark.addressDictionary;//详细地址信息字典,包含以下部分信息 // let name=placemark.name;//地名 // let thoroughfare=placemark.thoroughfare;//街道 // let subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等 // let locality=placemark.locality; // 城市 // let subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑 // let administrativeArea=placemark.administrativeArea; // 州 // let subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息 // let postalCode=placemark.postalCode; //邮编 // let ISOcountryCode=placemark.ISOcountryCode; //国家编码 // let country=placemark.country; //国家 // let inlandWater=placemark.inlandWater; //水源、湖泊 // let ocean=placemark.ocean; // 海洋 // let areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标 print(location,region,addressDic) } |