定位系统有许多三方可以继承,如果你的项目需要比较的完整的地位,建议使用三方地图,目前比较流行的是百度地图和高德地图,具体可以去Google一下,集成过程,查看文档集成,如果项目比较小,只需要简单定位,可以使用iOS系统自带的定位
首先需要引入 corelocation.framework 并且在需要的类里:
#import <AddressBook/AddressBook.h>
全局变量
@property(nonatomic,retain)CLLocationManager * locationManage;
遵守协议代理
@interface ViewController : UIViewController<CLLocationManagerDelegate>
假设点击一个button开始定位
- (IBAction)location:(id)sender {
[self createGPSMap];
}
-(void)createGPSMap
{
// 2、在Info.plist文件中添加如下配置:
// (1)NSLocationAlwaysUsageDescription YES
// (2)NSLocationWhenInUseUsageDescription YES
//初始化位置服务
_locationManage = [[CLLocationManager alloc] init];
// 设置定位精度
_locationManage.desiredAccuracy = kCLLocationAccuracyBest ;
//要求CLLocationManager 对象返回的全部数据
_locationManage.distanceFilter = kCLDistanceFilterNone ;
//IOS8以后需要添加
// [_locationManage requestAlwaysAuthorization];//一直可以调用
// [_locationManage requestWhenInUseAuthorization];//需要的事后调用
_locationManage.delegate = self;
[_locationManage startUpdatingLocation];
}
这样系统开始定位,定位完成后调用协议方法
如果定位成功
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
CLLocation * newLocation = [locations lastObject];
/**
* 停止定位
*/
[_locationManage stopUpdatingLocation];
/**
* 取得经纬度
*/
CLLocationCoordinate2D coord2D = newLocation.coordinate ;
double latitude = coord2D.latitude ;
double longitude = coord2D.longitude;
NSLog(@"纬度=%f 经度=%f",latitude,longitude);
//取得精度
CLLocationAccuracy horizontal = newLocation.horizontalAccuracy ;
CLLocationAccuracy vertical = newLocation.verticalAccuracy ;
NSLog(@"水平方=%f 垂直方=%f",horizontal,vertical);
//取得高度
CLLocationDistance altitude = newLocation.altitude ;
NSLog(@"高度%f",altitude);
//取得此时此刻
NSDate *timestamp = [newLocation timestamp];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//设置时间格式
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
[dateFormat setAMSymbol:@"AM"];//显示中文,改成“上午”
[dateFormat setPMSymbol:@"PM"];
NSString * dateString = [dateFormat stringFromDate:timestamp];
NSLog(@"时间%@",dateString);
// ==============位置反编码==============
CLGeocoder * geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark * place in placemarks) {
NSLog(@"name=%@",place.name);//街道
NSLog(@"thoroughfare = %@",place.subAdministrativeArea);//子街道
NSLog(@"locality = %@",place.locality);//市
NSLog(@"sublocality%@",place.subLocality);//区
NSLog(@"country = %@",place.country);//国家
NSArray * allKeys = place.addressDictionary.allKeys;
for (NSString *key in allKeys) {
NSLog(@"key=%@, value= %@",key,place.addressDictionary[key]);
}
#pragma mark - 使用系统定义的字符串直接查询,记得导入AddressBook框架
NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey);
NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]);
NSString *city = place.locality;
if(city == nil)
{
city = place.addressDictionary[(NSString *)kABPersonAddressStateKey];
}
}
}];
}
如果定位失败,则
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error
{
NSLog(@"%@",[error description]);
}
最后
-(void)dealloc
{
_locationManage = nil;
}
应用首次启动会提示用户,是否允许定位
如果不同意需要则无法定位,需要修改可以去设置里更改