123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- //
- // KKWLocationService.swift
- // SolarLamp
- //
- // Created by liaolj on 16/11/20.
- // Copyright © 2016年 SolarLamp. All rights reserved.
- //
- import UIKit
- import MapKit
- class KKWLocationService: NSObject {
- var locationService: CLLocationManager!
- typealias coordinationCallBack = ((CLLocationCoordinate2D)->())?
- var callBack:coordinationCallBack!
-
- typealias currentCityCallBack = ((NSString)->())?
- var cityCallBack :currentCityCallBack!
-
- static let shareInstance = KKWLocationService()
-
- override init() {
- super.init()
- locationService = CLLocationManager()
- locationService.delegate = self
- //设置定位精确度
- locationService.desiredAccuracy = kCLLocationAccuracyBest
- //设置定位请求状态
- if #available(iOS 8.0, *) {
- locationService.requestWhenInUseAuthorization()
- }else {
- print("定位失败")
- }
-
- if #available(iOS 9.0, *) {
- locationService.allowsBackgroundLocationUpdates = false
- } else {
- // Fallback on earlier versions
- }
- // locationService.delegate = self
-
-
- if CLLocationManager.locationServicesEnabled() {
- start()
- }else {
- print("没有定位服务")
- }
- }
-
- func start() {
-
- locationService.startUpdatingLocation()
- }
-
-
- //
- // func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
- // //print("didUpdateUserLocation lat:\(userLocation.location.coordinate.latitude) lon:\(userLocation.location.coordinate.longitude)")
- //// let coordinate = userLocation.location.coordinate
- //// callBack?(coordinate)
- //// locationService.stopUserLocationService()
- // }
-
- }
- extension KKWLocationService: CLLocationManagerDelegate {
-
- func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
- print(error)
- }
-
- //定位更新地理信息调用的代理方法
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- // print()
- if let coordinate = manager.location?.coordinate {
- //判断是不是属于国内范围 在之外
- if EMCoordinateTransform.isLocationOut(ofChina: coordinate){
- callBack?(coordinate)
- }else{
- let coord = EMCoordinateTransform.transformFromWGS(toGCJ: coordinate)
- callBack?(coord)
- }
-
-
-
- }
- if locations.count > 0 {
- let location = locations.last
- let clGeoCoder = CLGeocoder()
- let cl = CLLocation(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
-
- clGeoCoder.reverseGeocodeLocation(cl) { (placemarks, error) in
- if placemarks != nil {
- let plackmark = placemarks?.last
- let addressDic:NSDictionary = plackmark!.addressDictionary as! [String:AnyObject] as NSDictionary
- let City:NSString = addressDic.object(forKey: "City") as! NSString
- print("当前城市 --\(City)")
- // self.cityCallBack?(City)
- }
- }
- }
-
- manager.stopUpdatingLocation()
- }
- }
- // WGS04 To GCJ02(火星坐标)
- extension KKWLocationService{
-
- func transformFromWGSToGCJ(wgsLoc:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
- let a = 6378245.0;
- let ee = 0.00669342162296594323;
- let pi = Double.pi
-
- var adjustLoc :CLLocationCoordinate2D?
- if isLocationOutChina(location: wgsLoc) { //在国外
- adjustLoc = wgsLoc
- } else {
- var adjustLat = transformLatitude(x: wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
- var adjustLon = transformLongitude(x: wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
- let radLat = wgsLoc.latitude / 180.0 * pi;
- var magic = sin(radLat);
- magic = 1 - ee * magic * magic;
- let sqrtMagic = sqrt(magic);
- adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
- adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
- adjustLoc!.latitude = wgsLoc.latitude + adjustLat;
- adjustLoc!.longitude = wgsLoc.longitude + adjustLon;
-
-
- }
- return adjustLoc!
- }
-
- //判断是不是在中国
- func isLocationOutChina(location:CLLocationCoordinate2D) -> Bool {
- if location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271 {
- return true
- }
- return false
- }
-
- //转纬度
- func transformLatitude(x : Double, y :Double) -> Double {
- let pi = Double.pi
- var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
- lat += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
- lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0
- lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0
- return lat
- }
-
- //转经度
- func transformLongitude(x:Double, y:Double) -> Double {
- let pi = Double.pi
- var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
- lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
- lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
- lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
- return lon;
-
-
- }
-
- }
|