swift LocationManager 坐标偏移 WGS-84转GCJ-02(火星坐标)

import Foundation

import MapKit

class LocationUtils {
    
    class func isCoordinateOutOfChina(coordinate:CLLocationCoordinate2D) -> Bool{
        return coordinate.longitude < 72.004 || coordinate.longitude > 137.8347 || coordinate.latitude < 0.8293 || coordinate.latitude > 55.8271
    }
    
    class func transformToGCJ(fromWGS wgsLoc:CLLocationCoordinate2D) ->CLLocationCoordinate2D{
        let a = 6378245.0;
        let ee = 0.00669342162296594323;
        let pi = 3.14159265358979324;
        
        func transformLat(x:CLLocationDegrees,y:CLLocationDegrees) -> CLLocationDegrees {
            var lat:CLLocationDegrees = -100.0 + 2.0 * x + 3.0 * y
            lat += 0.2 * y * y
            lat += 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 transformLog(x:CLLocationDegrees,y:CLLocationDegrees) -> CLLocationDegrees {
            var lon:CLLocationDegrees = 300.0 + x + 2.0 * y + 0.1 * x * x
            lon += 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;
        }


        var adjustLat = transformLat(wgsLoc.longitude - 105.0, wgsLoc.latitude - 35.0);
        var adjustLon = transformLog(wgsLoc.longitude - 105.0 ,wgsLoc.latitude - 35.0);
        var radLat = wgsLoc.latitude / 180.0 * pi;
        var magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        var sqrtMagic = sqrt(magic);
        adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        
        return CLLocationCoordinate2D(latitude: wgsLoc.latitude + adjustLat, longitude: wgsLoc.longitude + adjustLon);
    }
}

public extension CLLocationCoordinate2D {
    func adjust() ->CLLocationCoordinate2D {
        return LocationUtils.isCoordinateOutOfChina(self) ? self : LocationUtils.transformToGCJ(fromWGS: self)
    }
}

直接贴代码,参考了

http://blog.csdn.net/hitwhylz/article/details/23019181

简单封装了下,

工具类+扩展类,适用于各种地方,建议大家把这个直接放到自己的library里面

用的时候这样:

var newLocation = locations.last as CLLocation
var adjustedCoordinate = newLocation.coordinate.adjust()
var region = MKCoordinateRegionMakeWithDistance(adjustedCoordinate, 100, 100)
mapView.setRegion(region, animated: true)

本来想扩展CLLocation 的 convention init 函数,直接修改接收到的location,但它里面的init 都是protected,没法实现,算了

 

posted @ 2014-11-16 22:15  1987zfp  阅读(293)  评论(0)    收藏  举报