天天看点

iOS获取用户位置(续)

前面尝试了mapkit+google,百度地图api 2种方式实现用户定位,最后一种使用的是CLLocation获取火星坐标让后修正的方案,因为修正是本地,这是目前最适合我们应用的方式之一。这种做法的优势是只要定位打开并允许应用获取你的位置,就能及时获取你的经纬度(google坐标),代码如下:

locManager = [[CLLocationManager alloc] init];
        locManager.delegate = self;
[locManager startUpdatingLocation];

#pragma mark 获取位置,这里得到的是火星坐标

- (void)locationManager:(CLLocationManager *)manager
	 didUpdateLocations:(NSArray *)locations
{
    if (locations.count == 0)
    {
        return;
    }
    CLLocation *oneLocation = [locations objectAtIndex:0];
}
//火星坐标纠正算法
- (CLLocationCoordinate2D)transformWglat:(double) wgLat andWglon:(double)wgLon
{
    double mgLat;
    double mgLon;
    if ([self outOfChinaWithLat:wgLat andLng:wgLon]) {
        mgLat = wgLat;
        mgLon = wgLon;
        
        CLLocationCoordinate2D coor2d = CLLocationCoordinate2DMake(mgLat, mgLon);
        return coor2d;
    }
    
    double dLat = [self transformLatx:wgLon - 105.0 andLaty:wgLat - 35.0];
    double dLon = [self transformLngx:wgLon - 105.0 andLngy:wgLat - 35.0];
    double radLat = wgLat / 180.0 * pi;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
    mgLat = wgLat + dLat;
    mgLon = wgLon + dLon;
    
    CLLocationCoordinate2D coor2d = CLLocationCoordinate2DMake(mgLat, mgLon);
    return coor2d;
}



- (BOOL)outOfChinaWithLat:(double)lat andLng:(double)lon
{
    if (lon < 72.004 || lon > 137.8347)
        return YES;
    if (lat < 0.8293 || lat > 55.8271)
        return YES;
    return NO;
}

-(double)transformLatx:(double)_x  andLaty:(double)_y
{
    
    
    double ret = -100.0 + 2*_x + 3.0 * _y + 0.2 * _y * _y + 0.1 * _x * _y + 0.2 * sqrt(fabs(_x));
    
    ret += (20.0 * sin(6.0 * _x * pi) + 20.0 * sin(2.0 * _x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(_y * pi) + 40.0 * sin(_y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * sin(_y / 12.0 * pi) + 320 * sin(_y * pi / 30.0)) * 2.0 / 3.0;
    
    return ret;
    
}

- (double)transformLngx:(double)_x andLngy:(double)_y
{
    double ret = 300.0 + _x + 2.0 * _y + 0.1 * _x * _x + 0.1 * _x * _y + 0.1 *sqrt(fabs(_x));
    ret += (20.0 * sin(6.0 * _x * pi) + 20.0 * sin(2.0 * _x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(_x * pi) + 40.0 * sin(_x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * sin(_x / 12.0 * pi) + 300.0 * sin(_x / 30.0 * pi)) * 2.0 / 3.0;
    return ret;
}