iOS 位置相关
来源:互联网 发布:玉溪超市软件 编辑:程序博客网 时间:2024/06/14 22:31
后台定位
CLLocationManager // 定位管理器
关键设置项:
desiredAccuracy //准确度
distanceFilter //采集范围
回调方法:
//位置按照设置,发生变化时触发此方法func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
注意事项:
iOS >= 7.0时,Capabilities中配置开启后台定位功能
iOS >= 8.0时,需要主动请求权限,并且在plist文件中配置NSLocationAlwaysUsageDescription、NSLocationWhenInUseUsageDescription
CLGeocoder //位置转码器
关键方法
//根据地理位置名称获得对应的CLPlacemark类geocodeAddressString(addressString: String, completionHandler: CLGeocodeCompletionHandler)//根据地理坐标获得对应的CLPlacemark类reverseGeocodeLocation(location: CLLocation, completionHandler: CLGeocodeCompletionHandler)
地图定位
MKMapView
关键设置项
showsScale //是否显示比例尺
showsCompass //是否显示指南针
showsTraffic //是否显示路况
mapType //地图模式:普通模式:Standard;卫星模式:Satellite;混合模式:Hybrid;卫星三维模式:SatelliteFlyover;混合三维模式:HybridFlyover
camera.pitch //摄像头角度
centerCoordinate //地图中心点
回调方法
//当位置发生变化时,触发此方法 optional public func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation)//自定义大头针optional public func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?//点击大头针 optional public func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)//规划的路线 optional public func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
关键方法
粗略计算预计时间距离
//设置起始点 let request = MKDirectionsRequest() let source = MKMapItem(placemark: MKPlacemark(coordinate:self.currentLocation!, addressDictionary: nil)) request.source = source //设置终点 let destination = MKMapItem(placemark: MKPlacemark(coordinate:coordinate, addressDictionary: nil)) request.destination = destination //type值为以下一种: //驾车:MKDirectionsTransportType.Automobile //步行:MKDirectionsTransportType.Walking //交通:MKDirectionsTransportType.Transit //随意:MKDirectionsTransportType.Any request.transportType = type let calcuteDirections = MKDirections(request: request) let calculateETA = MKDirections(request:request) //计算驾车或步行时间 calcuteDirections.calculateDirectionsWithCompletionHandler { (response, error) in if (error == nil) { for route in response!.routes { //添加线路 self.locationMapView.addOverlay(route.polyline) } } } //计算公共交通时间 calculateETA.calculateETAWithCompletionHandler { response, error in if error == nil { if let r = response { self.etaInfoAlert(r) } } }
导航
苹果地图
var launchOptions:[String : AnyObject]?if (r.transportType == MKDirectionsTransportType.Automobile) { launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving] } else if (r.transportType == MKDirectionsTransportType.Walking) { launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking] } else if (r.transportType == MKDirectionsTransportType.Transit) { launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit] } r.destination.openInMapsWithLaunchOptions(launchOptions)
百度地图
if (r.transportType == MKDirectionsTransportType.Automobile) { modelString = "driving" } else if (r.transportType == MKDirectionsTransportType.Walking) { modelString = "walking" } else if (r.transportType == MKDirectionsTransportType.Transit) { modelString = "transit" } let urlString = NSString(format: "baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=%@&src=pylocation",(self.currentLocation?.latitude)!,(self.currentLocation?.longitude)!, r.destination.placemark.coordinate.latitude, r.destination.placemark.coordinate.longitude,modelString!) UIApplication.sharedApplication().openURL(NSURL(string: urlString as String)!)
高德地图
if (r.transportType == MKDirectionsTransportType.Automobile) { model = 0 } else if (r.transportType == MKDirectionsTransportType.Walking) { model = 2 } else if (r.transportType == MKDirectionsTransportType.Transit) { model = 1 } let urlString = NSString(format: "iosamap://path?sourceApplication=pylocation&sid=currentlocation&did=destination&dlat=%f&dlon=%f&dev=0&t=%d&m=0",r.destination.placemark.coordinate.latitude,r.destination.placemark.coordinate.longitude,model) UIApplication.sharedApplication().openURL(NSURL(string: urlString as String)!)
demo GitHub地址:https://github.com/piang/iOSLocation
0 0
- iOS 位置相关
- iOS 位置与地图相关
- 谈谈iOS下视图、坐标、位置相关等基础概念
- 谈谈iOS下视图、坐标、位置相关等基础概念
- iOS LBS相关: 定位和中国特色的位置偏移纠正
- iPhone/iOS开启个人热点的相关位置调整小结
- QT位置相关函数
- Qt5位置相关函数
- Qt5位置相关函数
- Qt5位置相关函数
- 相关资源位置
- 定期iOS的后台位置更新-- 相关资料收集,待有时间进行整理
- 位置无关码,位置相关码
- IOS相关
- ios相关
- iOS相关
- ios 相关
- iOS后台报告位置
- Linux内核学习笔记三——进程调度
- mac安装pycham组件
- linux系统下gcc环境配置
- 如何进行自我介绍
- 告诫编程的自己
- iOS 位置相关
- linux 下串口工具minicom的使用
- J辉定时关机初步完成
- WM_CTLCOLOR消息的用法。有关控件…
- [原创]J辉定时关机v1.01修正版
- CCF201604-3 路径解析
- mfc实现托盘图标
- js 实现 input 文件的内容显示
- Linux内核学习笔记二——进程