由于欧美斯项目需要签到功能,因此需要给后台传一个当前位置的经纬度,以下是获取经纬度的方法
1>导入CoreLocation.frameWork
2>引入头文件,并遵循协议
#import <CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>
3>代码
@interface YYAboutUsViewController ()<UIWebViewDelegate,CLLocationManagerDelegate>
{
UIWebView *_webView;
NSString * _currentLatitude; //当前位置的纬度
NSString * _currentLongitude; //当前位置的经度
CLLocationManager *_locManager;
}
@end
@implementation YYAboutUsViewController
- (void)viewWillAppear:(BOOL)animated
{
//实例化一个位置管理器
_locManager = [[CLLocationManager alloc] init];
_locManager.delegate = self;
// 设置定位精度
// kCLLocationAccuracyNearestTenMeters:精度10米
// kCLLocationAccuracyHundredMeters:精度100 米
// kCLLocationAccuracyKilometer:精度1000 米
// kCLLocationAccuracyThreeKilometers:精度3000米
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用
_locManager.desiredAccuracy = kCLLocationAccuracyBest;
// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
// 它的单位是米,这里设置为至少移动1000再通知委托处理更新;
_locManager.distanceFilter = 1000.0f; // 如果设为kCLDistanceFilterNone,则每秒更新一次;
//判断手机定位是否开启
// 开启定位:设置 > 隐私 > 位置 > 定位服务
if ([CLLocationManager locationServicesEnabled]) {
// 启动位置更新
// 开启位置更新需要与服务器进行轮询所以会比较耗电,在不需要时用stopUpdatingLocation方法关闭;
[_locManager startUpdatingLocation];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请开启定位" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"guanyu_nav"] forBarMetrics:UIBarMetricsDefault];
if (Device_Is_IPhone5) {
_webView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64)];
}else{
_webView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44)];
}
[self.view addSubview:_webView];
}
#pragma mark -CLLocationManagerDelegate
// 地理位置发生改变时触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// 获取经纬度
NSLog(@"纬度:%f",newLocation.coordinate.latitude);
NSLog(@"经度:%f",newLocation.coordinate.longitude);
//加载网络数据
_webView.delegate = self;
//aboutOMS.html
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?latitude=%f&longitude=%f",kBaseUrl,@"7.aspx",newLocation.coordinate.latitude,newLocation.coordinate.longitude]];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
// 停止位置更新
[manager stopUpdatingLocation];
}
#pragma mark -webView的代理方法-
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[MBProgressHUD showWithText:@"加载中..." toView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
@end