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
原创粉丝点击