你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中定位以後地位坐標及轉換為火星坐標的辦法

iOS中定位以後地位坐標及轉換為火星坐標的辦法

編輯:IOS開發綜合

定位和地位信息獲得
定位和反查地位信息要加載兩個靜態庫 CoreLocation.framework 和 MapKit.framework 一個獲得坐標一個供給反查

// appDelgate.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
 
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,MKReverseGeocoderDelegate>
 
@property (strong, nonatomic) UIWindow *Window;
 
@end

 
#import "AppDelegate.h"
 
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.Window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    button.frame = CGRectMake(0, 100, 100, 30);
    [button setTitle:@"定位" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 320, 30)];
    label.tag = 101;
    label.text = @"期待定位中....";
    [self.window addSubview:label];
    [label release];
    [self.window addSubview:button];
    return YES;
 
}
 
-(void) test {
    
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    // 設置定位精度,十米,百米,最好
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    locationManager.delegate = self;
    
    // 開端不時定位
    [locationManager startUpdatingLocation];
}
 
// 毛病信息
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error");
}
 
// 6.0 以上挪用這個函數
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
    NSLog(@"%d", [locations count]);
    
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"舊的經度:%f,舊的緯度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
    
//    CLLocation *newLocation = locations[1];
//    CLLocationCoordinate2D newCoordinate = newLocation.coordinate;
//    NSLog(@"經度:%f,緯度:%f",newCoordinate.longitude,newCoordinate.latitude);
    
    // 盤算兩個坐標間隔
    //    float distance = [newLocation distanceFromLocation:oldLocation];
    //    NSLog(@"%f",distance);
    
    [manager stopUpdatingLocation];
    
    //------------------地位反編碼---5.0以後應用-----------------
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error){
                       
                       for (CLPlacemark *place in placemarks) {
                           UILabel *label = (UILabel *)[self.window viewWithtag:101];
                           label.text = place.name;
                           NSLog(@"name,%@",place.name);                       // 地位名
//                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
//                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
//                           NSLog(@"locality,%@",place.locality);               // 市
//                           NSLog(@"subLocality,%@",place.subLocality);         // 區
//                           NSLog(@"country,%@",place.country);                 // 國度
                       }
                       
                   }];
    
}
 
// 6.0 挪用此函數
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"%@", @"ok");
}
 
 
@end

轉換為火星坐標
這個寫的公共類叫做: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 百度location;

    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;

    百度location=[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坐標轉換成火星坐標 (谷歌坐標)

-(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)百度GPS MKMapView:(MKMapView*)myMapView{

    CLLocationCoordinate2D 谷歌GPS;

    谷歌GPS=[self hhTrans_GCGPS:百度GPS];//轉換為百度

    [self SetPurchaseMapPoint:谷歌GPS MKMapView:myMapView];

}

//把百度地圖轉換成谷歌地圖--火星坐標

-(CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)百度Gps

{

    CLLocationCoordinate2D 谷歌Gps;

    double bd_x=百度Gps.longitude - 0.0065;

    double bd_y=百度Gps.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);

    谷歌Gps.longitude = z * cos(theta);

    谷歌Gps.latitude = z * sin(theta);

    return 谷歌Gps;

}

 

-(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中定位以後地位坐標及轉換為火星坐標的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved