iOS开发中最基本的位置功能实现示例

2020-01-14 16:25:30王冬梅


// 开始定位 
[_locationManage startUpdatingLocation]; 

[_locationManage release]; 



- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)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对象 
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 *placemarks, NSError *error) { 

for (CLPlacemark * place in placemarks) { 

NSLog(@"name = %@",place.name); // 位置名 
NSLog(@"thoroughfare = %@",place.thoroughfare); // 街道 
NSLog(@"subAdministrativeArea = %@",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框架