CLLocationManager *locationManager;//定义Manager
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
}else {
//提示用户无法进行定位操作
}
// 开始定位
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//latitude和lontitude均为NSString型变量
//纬度
self.latitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.latitude];
//经度
self.longitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.longitude];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
self.latitude = coor.latitude;
self.longitude = coor.longitude;
//[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有