ios开发中使用系统定位服务怎么反检索位置

高分请问一下,ios开发中使用系统定位服务怎么反检索位置
最新回答
会笑才不是傻冒

2024-11-02 06:36:30

@property (strong, nonatomic) CLLocationManager *mgr;//定位
-(NSDictionary *)getIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}

-(void)locationCityName
{
// 1.获取用户的授权状态(iOS7只要使用到定位,就会直接请求授权)
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
/*
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
[mgr requestAlwaysAuthorization];
}
*/
if ([self.mgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.mgr requestAlwaysAuthorization];
}
}
// 2.开始定位(当调用该方法,系统就会不停的更新用户的位置)
[self.mgr startUpdatingLocation];
}
//CLLocationManagerDelegate的代理方法
#pragma mark - 懒加载
- (CLLocationManager *)mgr
{
if (_mgr == nil)
{
self.mgr = [[CLLocationManager alloc] init];
// 设置代理,在代理方法中可以拿到用户的位置
self.mgr.delegate = self;
// 设置定位的精度(精度越高越耗电)
self.mgr.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
// 设置当用户移动的时候,重新来定位
self.mgr.distanceFilter = 10.0;
}
return _mgr;
}
//更新位置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
{
if (array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
//将获得的所有信息显示到label上
NSLog(@"%@",placemark.name);
//获取城市
NSString *city = placemark.locality;
if (!city)
{
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"----city---- %@ ",city);
// NSLog(@"-----------%@-",placemark)
[UserDefaultsData setAddressesCity:city];
}else
{
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if (error.code == kCLErrorDenied)
{
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
}