//
// ViewController.swift
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 年 HEYANG. All rights reserved.
//
import UIKit
// 需要导入CoreLocation框架
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
// 声明一个全局变量
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
// 设置定位的精确度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// 设置定位变化的最小距离 距离过滤器
locationManager.distanceFilter =
// 设置请求定位的状态
if #available(iOS ., *) {
locationManager.requestWhenInUseAuthorization()
} else {
// Fallback on earlier versions
print("hello")
}//这个是在ios之后才有的
// 设置代理为当前对象
locationManager.delegate = self;
if CLLocationManager.locationServicesEnabled(){
// 开启定位服务
locationManager.startUpdatingLocation()
}else{
print("没有定位服务")
}
}
// 定位失败调用的代理方法
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
// 定位更新地理信息调用的代理方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count >
{
let locationInfo = locations.last!
let alert:UIAlertView = UIAlertView(title: "获取的地理坐标",
message: "经度是:\(locationInfo.coordinate.longitude),维度是:\(locationInfo.coordinate.latitude)",
delegate: nil, cancelButtonTitle: "是的")
alert.show()
}
}
}
//
// 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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有