配色: 字号:
iOS中定位当前位置坐标及转换为火星坐标的方法
2016-12-08 | 阅:  转:  |  分享 
  
iOS中定位当前位置坐标及转换为火星坐标的方法



这篇文章主要介绍了iOS中获取当前位置坐标及转换为火星坐标的方法,这里的火星坐标指的是我国专门研制的一种加密的坐标系统...需要的朋友可以参考下



定位和位置信息获取

定位和反查位置信息要加载两个动态库CoreLocation.framework和MapKit.framework一个获取坐标一个提供反查





复制代码代码如下:





//appDelgate.h

#import

#import

#import



@interfaceAppDelegate:UIResponder



@property(strong,nonatomic)UIWindowwindow;



@end







复制代码代码如下:





#import"AppDelegate.h"





@implementationAppDelegate



-(BOOL)application:(UIApplication)applicationdidFinishLaunchingWithOptions:(NSDictionary)launchOptions

{

self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

//Overridepointforcustomizationafterapplicationlaunch.

self.window.backgroundColor=[UIColorwhiteColor];

[self.windowmakeKeyAndVisible];

UIButtonbutton=[UIButtonbuttonWithType:UIButtonTypeContactAdd];

button.frame=CGRectMake(0,100,100,30);

[buttonsetTitle:@"定位"forState:UIControlStateNormal];

[buttonaddTarget:selfaction:@selector(test)forControlEvents:UIControlEventTouchUpInside];



UILabellabel=[[UILabelalloc]initWithFrame:CGRectMake(0,150,320,30)];

label.tag=101;

label.text=@"等待定位中....";

[self.windowaddSubview:label];

[labelrelease];

[self.windowaddSubview:button];

returnYES;



}



-(void)test{



CLLocationManagerlocationManager=[[CLLocationManageralloc]init];

//设置定位精度,十米,百米,最好

[locationManagersetDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

locationManager.delegate=self;



//开始时时定位

[locationManagerstartUpdatingLocation];

}



//错误信息

-(void)locationManager:(CLLocationManager)managerdidFailWithError:(NSError)error{

NSLog(@"error");

}



//6.0以上调用这个函数

-(void)locationManager:(CLLocationManager)managerdidUpdateLocations:(NSArray)locations{



NSLog(@"%d",[locationscount]);



CLLocationnewLocation=locations[0];

CLLocationCoordinate2DoldCoordinate=newLocation.coordinate;

NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);



//CLLocationnewLocation=locations[1];

//CLLocationCoordinate2DnewCoordinate=newLocation.coordinate;

//NSLog(@"经度:%f,纬度:%f",newCoordinate.longitude,newCoordinate.latitude);



//计算两个坐标距离

//floatdistance=[newLocationdistanceFromLocation:oldLwww.hunanwang.netocation];

//NSLog(@"%f",distance);



[managerstopUpdatingLocation];



//------------------位置反编码---5.0之后使用-----------------

CLGeocodergeocoder=[[CLGeocoderalloc]init];

[geocoderreverseGeocodeLocation:newLocation

completionHandler:^(NSArrayplacemarks,NSErrorerror){



for(CLPlacemarkplaceinplacemarks){

UILabellabel=(UILabel)[self.windowviewWithTag: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)managerdidUpdateToLocation:(CLLocation)newLocationfromLocation:(CLLocation)oldLocation{

NSLog(@"%@",@"ok");

}





@end



转换为火星坐标

这个写的公共类叫做:GPScombineClass类主要展示GPS位置的定位,GPS坐标的获取,然后从手机坐标转换成火星坐标,继而在需要的情况下,由火星转百度,百度转火星的详细算法;



在GPScombineClass.h中





复制代码代码如下:





#import



#import



#import"CSqlite.h"



#import



@interfaceGPScombineClass:NSObject{



CLLocationManagerlocationManager;



CSqlitem_sqlite;







UILabelm_locationName;



MKMapViewmainMapView;



@publicCLLocationCoordinate2Dbaidulocation;



CLLocationCoordinate2DdeleeverLocation;



}



-(void)OpenGPSmapView;



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



-(void)setMyMapPonitByMKMapView:(MKMapView)MyMap;



@end







复制代码代码如下:





@interfacePOI:NSObject{







CLLocationCoordinate2Dcoordinate;



NSStringsubtitle;



NSStringtitle;



}







@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;



@property(nonatomic,retain)NSStringsubtitle;



@property(nonatomic,retain)NSStringtitle;







-(id)initWithCoords:(CLLocationCoordinate2D)coords;







@end









在GPScombineClass.m中





复制代码代码如下:





#import"GPScombineClass.h"



constdoublex_pi=3.141592653589793243000.0/180.0;



@implementationGPScombineClass



-(void)OpenGPSmapView{



m_sqlite=[[CSqlitealloc]init];



[m_sqliteopenSqlite];



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



locationManager=[[CLLocationManageralloc]init];



locationManager.delegate=self;



locationManager.distanceFilter=0.5;



locationManager.desiredAccuracy=kCLLocationAccuracyBest;



[locationManagerstartUpdatingLocation];//开始定位



}







NSLog(@"GPS启动");



}







//定位成功时调用



-(void)locationManager:(CLLocationManager)manager



didUpdateToLocation:(CLLocation)newLocation



fromLocation:(CLLocation)oldLocation



{



CLLocationCoordinate2Dmylocation=newLocation.coordinate;//手机GPS







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



deleeverLocation=mylocation;



baidulocation=[selfhhTrans_bdGPS:mywww.visa158.comlocation];//转换成百度地图



/



//显示火星坐标



[selfSetMapPoint:mylocationMKMapView:mainMapView];







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



CLGeocodergeocoder=[[CLGeocoderalloc]init];



[geocoderreverseGeocodeLocation:newLocationcompletionHandler:^(NSArrayplacemarks,NSErrorerror)



{



if(placemarks.count>0)



{



CLPlacemarkplmark=[placemarksobjectAtIndex:0];







NSStringcountry=plmark.country;



NSStringcity=plmark.locality;











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



self->m_locationName.text=plmark.name;



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



}







NSLog(@"%@",placemarks);







}];







//[geocoderrelease];



/



}



//定位失败时调用



-(void)locationManager:(CLLocationManager)manager



didFailWithError:(NSError)error{



NSLog(@"定位失败");



}







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



-(CLLocationCoordinate2D)zzTransGPS:(CLLocationCoordinate2D)yGps



{



intTenLat=0;



intTenLog=0;



TenLat=(int)(yGps.latitude10);



TenLog=(int)(yGps.longitude10);



NSStringsql=[[NSStringalloc]initWithFormat:@"selectoffLat,offLogfromgpsTwherelat=%dandlog=%d",TenLat,TenLog];



NSLog(sql);



sqlite3_stmtstmtL=[m_sqliteNSRunSql:sql];



intoffLat=0;



intoffLog=0;



while(sqlite3_step(stmtL)==SQLITE_ROW)



{



offLat=sqlite3_column_int(stmtL,0);



offLog=sqlite3_column_int(stmtL,1);







}







yGps.latitude=yGps.latitude+offLat0.0001;



yGps.longitude=yGps.longitude+offLog0.0001;



returnyGps;











}



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



-(void)setMyMapPonitByMKMapView:(MKMapView)MyMap{



//显示火星坐标



[selfSetMapPoint:deleeverLocationMKMapView:MyMap];



MyMap=mainMapView;



}



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



-(void)SetMapPoint:(CLLocationCoordinate2D)myLocationMKMapView:(MKMapView)mapView



{



//POIm_poi=[[POIalloc]initWithCoords:myLocation];



//



//[mapViewaddAnnotation:m_poi];







MKCoordinateRegiontheRegion={{0.0,0.0},{0.0,0.0}};



theRegion.center=myLocation;



[mapViewsetZoomEnabled:YES];



[mapViewsetScrollEnabled:YES];



theRegion.span.longitudeDelta=0.01f;



theRegion.span.latitudeDelta=0.01f;



[mapViewsetRegion:theRegionanimated:YES];







}







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



-(CLLocationCoordinate2D)hhTrans_bdGPS:(CLLocationCoordinate2D)fireGps



{



CLLocationCoordinate2DbdGps;



doublehuo_x=fireGps.longitude;



doublehuo_y=fireGps.latitude;



doublez=sqrt(huo_xhuo_x+huo_yhuo_y)+0.00002sin(huo_yx_pi);



doubletheta=atan2(huo_y,huo_x)+0.000003cos(huo_xx_pi);



bdGps.longitude=zcos(theta)+0.0065;



bdGps.latitude=zsin(theta)+0.006;



returnbdGps;



}



#pragmamark显示商品信息



#pragmamark



-(void)showPurchaseOnMapByLocation:(CLLocationCoordinate2D)baiduGPSMKMapView:(MKMapView)myMapView{



CLLocationCoordinate2DgoogleGPS;



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



[selfSetPurchaseMapPoint:googleGPSMKMapView:myMapView];



}



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



-(CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps



{



CLLocationCoordinate2DgoogleGps;



doublebd_x=baiduGps.longitude-0.0065;



doublebd_y=baiduGps.latitude-0.006;



doublez=sqrt(bd_xbd_x+bd_ybd_y)-0.00002sin(bd_yx_pi);



doubletheta=atan2(bd_y,bd_x)-0.000003cos(bd_xx_pi);



googleGps.longitude=zcos(theta);



googleGps.latitude=zsin(theta);



returngoogleGps;



}







-(void)SetPurchaseMapPoint:(CLLocationCoordinate2D)myLocationMKMapView:(MKMapView)mapView



{



POIm_poi=[[POIalloc]initWithCoords:myLocation];





[mapViewaddAnnotation:m_poi];





MKCoordinateRegiontheRegion={{0.0,0.0},{0.0,0.0}};



theRegion.center=myLocation;



[mapViewsetZoomEnabled:YES];



[mapViewsetScrollEnabled:YES];



theRegion.span.longitudeDelta=0.01f;



theRegion.span.latitudeDelta=0.01f;



[mapViewsetRegion:theRegionanimated:YES];}



@end





















献花(0)
+1
(本文系白狐一梦首藏)