900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > iOS定位及解析经纬度【Swift】

iOS定位及解析经纬度【Swift】

时间:2022-10-20 17:04:45

相关推荐

iOS定位及解析经纬度【Swift】

导入

import CoreLocationimport MapKit

实现CLLocationManagerDelegate代理

初始化位置信息

// MARK:初始化位置func initLocation() {locationManager.delegate = self//设置定位模式locationManager.desiredAccuracy = kCLLocationAccuracyBest//更新距离locationManager.distanceFilter = 5//发送授权申请locationManager.requestWhenInUseAuthorization()if (CLLocationManager.locationServicesEnabled()){//允许使用定位服务的话,开启定位服务更新locationManager.startUpdatingLocation()print("定位开始")}}

获取设备经纬度

//MARK:CLLocationManagerDelegatefunc locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {// var currLocation:CLLocation!初始化!!!//获取最新的坐标currLocation = locations.last!// //获取经度print("精度:\(currLocation.coordinate.longitude)")//获取纬度print("纬度:\(currLocation.coordinate.latitude)")// //获取海拔print("海拔:\(currLocation.altitude)")// //获取水平精度print("水平精度:\(currLocation.horizontalAccuracy)")// //获取垂直精度print("垂直精度:\(currLocation.verticalAccuracy)")// //获取方向print("方向:\(currLocation.course)")// //获取速度print("速度:\(currLocation.speed)")reverseGeocode()}

reverseGeocode() 方法为地址返编码

//MARK: 地理信息反编码func reverseGeocode(){let geocoder = CLGeocoder()let currentLocation = CLLocation(latitude: currLocation.coordinate.latitude, longitude: currLocation.coordinate.longitude)geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {(placemarks:[CLPlacemark]?, error:Error?) -> Void in//强制转成简体中文let array = NSArray(object: "zh-hans")UserDefaults.standard.set(array, forKey: "AppleLanguages")// //显示所有信息if error != nil {//print("错误:\(error.localizedDescription))")print("错误:\(error!.localizedDescription))")return}if let p = placemarks?[0]{print(p) //输出反编码信息var address = ""if let country = p.country {address.append("国家:\(country)\n")}if let administrativeArea = p.administrativeArea {address.append("省份:\(administrativeArea)\n")}if let subAdministrativeArea = p.subAdministrativeArea {address.append("其他行政区域信息(自治区等):\(subAdministrativeArea)\n")}if let locality = p.locality {address.append("城市:\(locality)\n")}if let subLocality = p.subLocality {address.append("区划:\(subLocality)\n")}if let thoroughfare = p.thoroughfare {address.append("街道:\(thoroughfare)\n")}if let subThoroughfare = p.subThoroughfare {address.append("门牌:\(subThoroughfare)\n")}if let name = p.name {address.append("地名:\(name)\n")}if let isoCountryCode = p.isoCountryCode {address.append("国家编码:\(isoCountryCode)\n")}if let postalCode = p.postalCode {address.append("邮编:\(postalCode)\n")}if let areasOfInterest = p.areasOfInterest {address.append("关联的或利益相关的地标:\(areasOfInterest)\n")}if let ocean = p.ocean {address.append("海洋:\(ocean)\n")}if let inlandWater = p.inlandWater {address.append("水源,湖泊:\(inlandWater)\n")}print("111----\(address)")} else {print("No placemarks!")}})}

// MARK:定位失败、重新授权定位func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {print("定位失败")}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。