Swift - 将定位获取的经纬度转换为城市名

本文介绍如何在iOS应用中集成定位服务,包括导入MapKit框架、配置权限描述、实现CLLocationManagerDelegate协议、获取地理位置信息以及经纬度转城市名等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.导入MapKit.framework框架


2.(非iOS8及之后版本跳过此步骤)在项目的info.plist下查看是否有以下两个属性

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

若无就手动添加!


3.在相应类导入相应文件  import CoreLocation 并遵守 CLLocationManagerDelegate 协议


4.开始添加一个全局属性并为它初始化

    //保存获取到的本地位置
    var currLocation : CLLocation!
    //用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向
    let locationManager : CLLocationManager = CLLocationManager()

    func initLocation() {
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        //设备使用电池供电时最高的精度
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //精确到1000米,距离过滤器,定义了设备移动后获得位置信息的最小距离
        locationManager.distanceFilter = kCLLocationAccuracyKilometer
        locationManager.startUpdatingLocation()
    }

5.实现相关的协议


    //MARK:- 实现CLLocationManagerDelegate协议
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        currLocation = locations.last as! CLLocation
        println(currLocation.coordinate.longitude)
        println(currLocation.coordinate.latitude)
        LonLatToCity()
    }
    
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
<span style="white-space:pre">	</span>//获取失败,可能是关闭了定位服务
        println(error)
    }

6.实现将经纬度转换为城市名的函数


    ///将经纬度转换为城市名
    func LonLatToCity() {
        let geocoder: CLGeocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(currLocation, completionHandler: { (var placemark: [AnyObject]!, var error: NSError!) -> Void in
            if (error == nil) {//转换成功,解析获取到的各个信息
                let array = placemark as NSArray
                let mark = array.firstObject as! CLPlacemark
                
                var city: String = (mark.addressDictionary as NSDictionary).valueForKey("City") as! String
                let country: NSString = (mark.addressDictionary as NSDictionary).valueForKey("Country") as! NSString
                let CountryCode: NSString = (mark.addressDictionary as NSDictionary).valueForKey("CountryCode") as! NSString
                let FormattedAddressLines: NSString = (mark.addressDictionary as NSDictionary).valueForKey("FormattedAddressLines")?.firstObject as! NSString
                let Name: NSString = (mark.addressDictionary as NSDictionary).valueForKey("Name") as! NSString
                var State: String = (mark.addressDictionary as NSDictionary).valueForKey("State") as! String
                let SubLocality: NSString = (mark.addressDictionary as NSDictionary).valueForKey("SubLocality") as! NSString
                
                //去掉“市”和“省”字眼
                //city = city.stringByReplacingOccurrencesOfString("市", withString: "")
                //State = State.stringByReplacingOccurrencesOfString("省", withString: "")
                
                
                
//                println(city)
    //            println(country)
    //            println(CountryCode)
    //            println(FormattedAddressLines)
    //            println(Name)
//                println(State)
    //            println(SubLocality)
            }else {
                //转换失败
            }
            
        })
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值