iOS中定位当前位置坐标及转换为火星坐标的方法
定位和位置信息获取
定位和反查位置信息要加载两个动态库CoreLocation.framework和MapKit.framework一个获取坐标一个提供反查
//appDelgate.h #import<UIKit/UIKit.h> #import<CoreLocation/CoreLocation.h> #import<MapKit/MapKit.h> @interfaceAppDelegate:UIResponder<UIApplicationDelegate,CLLocationManagerDelegate,MKReverseGeocoderDelegate> @property(strong,nonatomic)UIWindow*window; @end
#import"AppDelegate.h" @implementationAppDelegate -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]; //Overridepointforcustomizationafterapplicationlaunch. self.window.backgroundColor=[UIColorwhiteColor]; [self.windowmakeKeyAndVisible]; UIButton*button=[UIButtonbuttonWithType:UIButtonTypeContactAdd]; button.frame=CGRectMake(0,100,100,30); [buttonsetTitle:@"定位"forState:UIControlStateNormal]; [buttonaddTarget:selfaction:@selector(test)forControlEvents:UIControlEventTouchUpInside]; UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(0,150,320,30)]; label.tag=101; label.text=@"等待定位中...."; [self.windowaddSubview:label]; [labelrelease]; [self.windowaddSubview:button]; returnYES; } -(void)test{ CLLocationManager*locationManager=[[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]); CLLocation*newLocation=locations[0]; CLLocationCoordinate2DoldCoordinate=newLocation.coordinate; NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude); // CLLocation*newLocation=locations[1]; // CLLocationCoordinate2DnewCoordinate=newLocation.coordinate; // NSLog(@"经度:%f,纬度:%f",newCoordinate.longitude,newCoordinate.latitude); //计算两个坐标距离 // floatdistance=[newLocationdistanceFromLocation:oldLocation]; // NSLog(@"%f",distance); [managerstopUpdatingLocation]; //------------------位置反编码---5.0之后使用----------------- CLGeocoder*geocoder=[[CLGeocoderalloc]init]; [geocoderreverseGeocodeLocation:newLocation completionHandler:^(NSArray*placemarks,NSError*error){ for(CLPlacemark*placeinplacemarks){ UILabel*label=(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<Foundation/Foundation.h>
#import<CoreLocation/CoreLocation.h>
#import"CSqlite.h"
#import<MapKit/MapKit.h>
@interfaceGPScombineClass:NSObject<MKMapViewDelegate>{
CLLocationManager*locationManager;
CSqlite*m_sqlite;
UILabel*m_locationName;
MKMapView*mainMapView;
@publicCLLocationCoordinate2Dbaidulocation;
CLLocationCoordinate2DdeleeverLocation;
}
-(void)OpenGPSmapView;
//在地图上放上自己的位置--外接接口
-(void)setMyMapPonitByMKMapView:(MKMapView*)MyMap;
@end