下面是Objective-C的源码:
//
// ViewController.m
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 年 HEYANG. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
/** 全局定位对象 */
@property (nonatomic,strong)CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
// 设置定位精确度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 设置定位变化最小距离
locationManager.distanceFilter = ;
// 设置定位服务的使用状态
[locationManager requestWhenInUseAuthorization];
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}else{
NSLog(@"本机不支持定位服务功能");
}
self.locationManager = locationManager;
}
// 定位失败调用的代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"错误信息:%@",error);
}
// 定位数据更新调用的代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
if (locations.count > ) {
CLLocation* location = locations.lastObject;
CLLocationCoordinateD coordinateD = location.coordinate;
NSString* message = [NSString stringWithFormat:@"经度:%lf,维度是:%lf",coordinateD.longitude,coordinateD.latitude];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"显示当前位置的经纬度" message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
}
@end
以上是小编给大家分享的IOS入门笔记之地理位置定位系统,希望对大家有所帮助。
注:相关教程知识阅读请移步到IOS开发频道。










