服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS中定位当前位置坐标及转换为火星坐标的方法

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

2021-01-08 14:22常思过 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 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];

}

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

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

 

延伸 · 阅读

精彩推荐
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03