ISO GPS定位,座標轉換以及如何顯示

來源:互聯網
上載者:User

這個寫的公用類叫做:GPScombineClass類主要展示GPS位置的定位,GPS座標的擷取,然後從手機座標轉換成火星座標,繼而在需要的情況下,由火星轉百度 ,百度轉火星的詳細演算法;

 

在GPScombineClass.h中

 

#import <Foundation/Foundation.h>

#import <CoreLocation/CoreLocation.h>

#import "CSqlite.h"

#import <MapKit/MapKit.h>

@interface GPScombineClass : NSObject<MKMapViewDelegate>{

    CLLocationManager *locationManager;

    CSqlite *m_sqlite;

    

    UILabel *m_locationName;

    MKMapView *mainMapView;

@public CLLocationCoordinate2D baidulocation;

    CLLocationCoordinate2D deleeverLocation;

}

-(void)OpenGPSmapView;

//在地圖上放上自己的位置--外接介面

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

@end

@interface POI : NSObject <MKAnnotation> {

    

    CLLocationCoordinate2D coordinate;

    NSString *subtitle;

    NSString *title;

}

 

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic,retain) NSString *subtitle;

@property (nonatomic,retain) NSString *title;

 

-(id) initWithCoords:(CLLocationCoordinate2D) coords;

 

@end

 

在GPScombineClass.m中

 

 

#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];

}

//把百度地圖轉換成Google地圖--火星座標

-(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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.