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
上一篇: 香肠怎么吃好吃?好的原料才能做出好的菜
下一篇: PHP实现即时输出、实时输出内容方法