您的位置:首页 > 移动开发 > IOS开发

iOS中定位当前位置坐标及转换为火星坐标的方法

2016-02-27 09:18 2016 查看

#import "GPScombineClass.h"

const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

@implementation GPScombineClass

-(void)OpenGPSmapView{

    m_sqlite = [[CSqlite alloc]init];

    [m_sqlite openSqlite];

    if ([CLLocationManager locationServicesEnabled]) { // 检查定位服务是否可用

        locationManager = [[CLLocationManager alloc] init];

        locationManager.delegate = self;

        locationManager.distanceFilter=0.5;

        locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        [locationManager startUpdatingLocation]; // 开始定位

    }

   

    NSLog(@"GPS 启动");

}

 

// 定位成功时调用

- (void)locationManager:(CLLocationManager *)manager

    didUpdateToLocation:(CLLocation *)newLocation

           fromLocation:(CLLocation *)oldLocation

{

    CLLocationCoordinate2D mylocation = newLocation.coordinate;//手机GPS

   

    mylocation = [self zzTransGPS:mylocation];///转换成火星GPS

    deleeverLocation=mylocation;

    baidulocation=[self hhTrans_bdGPS:mylocation];//转换成百度地图

     /*

    //显示火星坐标

    [self SetMapPoint:mylocation MKMapView:mainMapView];

  

    /////////获取位置信息

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks,NSError *error)

     {

         if (placemarks.count >0   )

         {

             CLPlacemark * plmark = [placemarks objectAtIndex:0];

            

             NSString * country = plmark.country;

             NSString * city    = plmark.locality;

            

            

             NSLog(@"%@-%@-%@",country,city,plmark.name);

             self->m_locationName.text =plmark.name;

             NSLog(@"%@",self->m_locationName);

         }

        

         NSLog(@"%@",placemarks);

        

     }];

   

    //[geocoder release];

    */

}

// 定位失败时调用

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error {

    NSLog(@"定位失败");

}

 

//把手机GPS坐标转换成火星坐标 (google坐标)

-(CLLocationCoordinate2D)zzTransGPS:(CLLocationCoordinate2D)yGps

{

    int TenLat=0;

    int TenLog=0;

    TenLat = (int)(yGps.latitude*10);

    TenLog = (int)(yGps.longitude*10);

    NSString *sql = [[NSString alloc]initWithFormat:@"select offLat,offLog from gpsT where lat=%d and log = %d",TenLat,TenLog];

    NSLog(sql);

    sqlite3_stmt* stmtL = [m_sqlite NSRunSql:sql];

    int offLat=0;

    int offLog=0;

    while (sqlite3_step(stmtL)==SQLITE_ROW)

    {

        offLat = sqlite3_column_int(stmtL, 0);

        offLog = sqlite3_column_int(stmtL, 1);

       

    }

   

    yGps.latitude = yGps.latitude+offLat*0.0001;

    yGps.longitude = yGps.longitude + offLog*0.0001;

    return yGps;

   

   

}

//在地图上放上自己的位置--外接接口

-(void)setMyMapPonitByMKMapView:(MKMapView *)MyMap{

 //显示火星坐标

    [self SetMapPoint:deleeverLocation MKMapView:MyMap];

    MyMap=mainMapView;

}

//在地图上放上自己的位置

-(void)SetMapPoint:(CLLocationCoordinate2D)myLocation MKMapView:(MKMapView *)mapView

{

//    POI* m_poi = [[POI alloc]initWithCoords:myLocation];

//   

//    [mapView addAnnotation:m_poi];

   

    MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };

    theRegion.center=myLocation;

    [mapView setZoomEnabled:YES];

    [mapView setScrollEnabled:YES];

    theRegion.span.longitudeDelta = 0.01f;

    theRegion.span.latitudeDelta = 0.01f;

    [mapView setRegion:theRegion animated:YES];

   

}

 

//把火星坐标转换成百度坐标

-(CLLocationCoordinate2D)hhTrans_bdGPS:(CLLocationCoordinate2D)fireGps

{

    CLLocationCoordinate2D bdGps;

    double huo_x=fireGps.longitude;

    double huo_y=fireGps.latitude;

    double z = sqrt(huo_x * huo_x + huo_y * huo_y) + 0.00002 * sin(huo_y * x_pi);

    double theta = atan2(huo_y, huo_x) + 0.000003 * cos(huo_x * x_pi);

    bdGps.longitude = z * cos(theta) + 0.0065;

    bdGps.latitude = z * sin(theta) + 0.006;

    return bdGps;

}

#pragma mark 显示商品信息

#pragma mark

-(void)showPurchaseOnMapByLocation:(CLLocationCoordinate2D)baiduGPS MKMapView:(MKMapView*)myMapView{

    CLLocationCoordinate2D googleGPS;

    googleGPS=[self hhTrans_GCGPS:baiduGPS];//转换为百度

    [self SetPurchaseMapPoint:googleGPS MKMapView:myMapView];

}

//把百度地图转换成谷歌地图--火星坐标

-(CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps

{

    CLLocationCoordinate2D googleGps;

    double bd_x=baiduGps.longitude - 0.0065;

    double bd_y=baiduGps.latitude - 0.006;

    double z = sqrt(bd_x * bd_x + bd_y * bd_y) - 0.00002 * sin(bd_y * x_pi);

    double theta = atan2(bd_y, bd_x) - 0.000003 * cos(bd_x * x_pi);

    googleGps.longitude = z * cos(theta);

    googleGps.latitude = z * sin(theta);

    return googleGps;

}

 

-(void)SetPurchaseMapPoint:(CLLocationCoordinate2D)myLocation MKMapView:(MKMapView *)mapView

{

    POI* m_poi = [[POI alloc]initWithCoords:myLocation];

  
    [mapView addAnnotation:m_poi];
 

    MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };

    theRegion.center=myLocation;

    [mapView setZoomEnabled:YES];

    [mapView setScrollEnabled:YES];

    theRegion.span.longitudeDelta = 0.01f;

    theRegion.span.latitudeDelta = 0.01f;

    [mapView setRegion:theRegion animated:YES];}

@end

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 定位