这是在网上找的解决方法。。
//
// JZLocationConverter.h
// Run
//
// Created by Nick on 15/6/3.
// Copyright (c) 2015年 com.zywl.running. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface JZLocationConverter : NSObject
/**
* @brief 世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
*
* @param location 世界标准地理坐标(WGS-84)
*
* @return 中国国测局地理坐标(GCJ-02)<火星坐标>
*/
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
*
* ####此接口有1-2米左右的误差,需要精确定位情景慎用
*
* @param location 中国国测局地理坐标(GCJ-02)
*
* @return 世界标准地理坐标(WGS-84)
*/
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;
/**
* @brief 世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
*
* @param location 世界标准地理坐标(WGS-84)
*
* @return 百度地理坐标(BD-09)
*/
+(CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 中国国测局地理坐标(GCJ-02)<火星坐标> 转换成 百度地理坐标(BD-09)
*
* @param location 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* @return 百度地理坐标(BD-09)
*/
+(CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* @param location 百度地理坐标(BD-09)
*
* @return 中国国测局地理坐标(GCJ-02)<火星坐标>
*/
+(CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
*
* ####此接口有1-2米左右的误差,需要精确定位情景慎用
*
* @param location 百度地理坐标(BD-09)
*
* @return 世界标准地理坐标(WGS-84)
*/
+(CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;
@end
//
// Location.m
// Run
//
// Created by Nick on 15/5/28.
// Copyright (c) 2015年 com.zywl.running. All rights reserved.
//
#import "Location.h"
#import "JZLocationConverter.h"
static Location * shared = nil;
@implementation Location
+(id) shareManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (shared == nil)
{
shared = [[super allocWithZone:NULL] init];
}
});
return shared;
}
+(id)allocWithZone:(struct _NSZone *)zone
{
return [self shareManager];
}
-(id)copy
{
return self;
}
-(void)starManager
{
self.locationManager = [[CLLocationManager alloc] init];//创建位置管理器
//设置代理
self.locationManager.delegate = self;
double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
if(version>=8.0f){
[self.locationManager requestWhenInUseAuthorization];//添加这句
}
//设置定位精度
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//定位频率,每隔多少米定位一次
CLLocationDistance distance=10.0;//十米定位一次
self.locationManager.distanceFilter=distance;
//启动跟踪定位
[self.locationManager startUpdatingLocation];
}
///添加运动记录
-(NSMutableDictionary *)addHistory
{
NSMutableDictionary * par= [[NSMutableDictionary alloc]init];
[par setObject:[self setTime:[NSDate date]] forKey:@"start_time"];
[par setObject:@"0" forKey:@"distance"];
[par setObject:@"0" forKey:@"duration"];
return par;
}
///更新运动记录
-(NSMutableDictionary *)updateHistory
{
NSMutableDictionary * par= [[NSMutableDictionary alloc]init];
[par setObject:self.history_id forKey:@"history_id"];
[par setObject:[NSString stringWithFormat:@"%.2f",self.distance] forKey:@"distance"];
[par setObject:[NSString stringWithFormat:@"%d",self.duration] forKey:@"duration"];
return par;
}
///添加运动轨迹
-(NSMutableDictionary *)addTrace:(NSMutableArray *)array
{
NSMutableArray * arr = [self setPar:array];
NSMutableDictionary * par= [[NSMutableDictionary alloc]init];
[par setObject:self.history_id forKey:@"history_id"];
[par setObject:[arr objectAtIndex:3] forKey:@"trace_time"];
[par setObject:[arr objectAtIndex:0] forKey:@"latitude"];
[par setObject:[arr objectAtIndex:1] forKey:@"longitude"];
[par setObject:[arr objectAtIndex:2] forKey:@"altitude"];
return par;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
// coor = [self zzTransGPS:coor];///火星GPS
// NSLog(@"%f",coor.latitude);
// NSLog(@"%f",coor.longitude);
coor = [JZLocationConverter wgs84ToGcj02:coor];
self.latitude = coor.latitude;
self.longitude = coor.longitude;
// NSLog(@"%f",self.latitude);
// NSLog(@"%f",self.longitude);
//[self.locationManager stopUpdatingLocation];
}
-(CLLocationCoordinate2D)zzTransGPS:(CLLocationCoordinate2D)yGps
{
int TenLat=0;
int TenLog=0;
TenLat = (int)(yGps.latitude*10);
TenLog = (int)(yGps.longitude*10);
int offLat=0;
int offLog=0;
yGps.latitude = yGps.latitude+offLat*0.0001;
yGps.longitude = yGps.longitude + offLog*0.0001;
return yGps;
}
//调整时间格式
-(NSString * )setTime:(NSDate *)time
{
NSDateFormatter * outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString * timeStr = [outputFormatter stringFromDate:time];
timeStr=[NSString stringWithFormat:@"%@%%20%@",[timeStr substringToIndex:10],[timeStr substringFromIndex:11]];
return timeStr;
}
///设置参数形式
-(NSMutableArray *)setPar:(NSMutableArray *)array
{
NSMutableArray * parArray = [[NSMutableArray alloc]init];
int n = (int)[array count];
for (int i = 0; i < n; i++)
{
NSArray * array1 = [NSArray arrayWithArray:[array objectAtIndex:i]];
NSString * temp1 = @"";
for (NSString * str in array1)
{
// if (i == 3)
// {
// NSString * str1 = [NSString stringWithFormat:@"%@%%20%@",[str substringToIndex:10],[str substringFromIndex:11]];
// temp1 = [temp1 stringByAppendingFormat:@",%@",str1];
//
// }else
temp1 = [temp1 stringByAppendingFormat:@",%@",str];
}
[parArray addObject:temp1];
}
return parArray;
}
//根据经纬度算距离
-(double)GetDistance:(NSString*)lat1 :(NSString *)lon1 :(NSString*)lat2 :(NSString*)lon2
{
CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:[lat1 doubleValue] longitude:[lon1 doubleValue]];
CLLocation *portland = [[CLLocation alloc] initWithLatitude:[lat2 doubleValue]longitude:[lon2 doubleValue]];
CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];
//MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];
//formatter.units = MKDistanceFormatterUnitsMetric;
// NSLog(@"%0.0f m",[[NSString stringWithFormat:@"%0.0f",distance] doubleValue]);
return [[NSString stringWithFormat:@"%.2f",distance] doubleValue];
//return dist;
}
///将时间转化成秒
-(int)changeSecond:(NSString *)time
{
int i = 0;
NSArray * temp = [time componentsSeparatedByString:@":" ];
int m = [[temp objectAtIndex:0] intValue];
int s = [[temp objectAtIndex:1] intValue];
i = m * 60 + s;
return i;
}
@end