CoreLocation+MapKit系统定位(含坐标以及详细地址)

摘要:
iOS 8之后出现了一些新配置[self.managerrequestWhenUseAuthorization];在信息中,将NSLocationWhenUseUsageDescriptionBOOLYENSLocationAlwaysUsageDescriptions字符串“Prompt Description”添加到plist文件中。记得添加依赖库CoreLocation.frameworkMapKit.fra

iOS8 之后出现一些新的配置

[self.manager requestWhenInUseAuthorization];
 并且在info.plist文件中增加
 NSLocationWhenInUseUsageDescription  BOOL YES
 NSLocationAlwaysUsageDescription         string “提示描述”
记得加依赖库
CoreLocation.framework MapKit.framework

创建MapView

if (_mapView == nil) {
    _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 0.5, 0.5)];
    //        _mapView.userTrackingMode = MKUserTrackingModeFollow;
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    [self.view addSubview:_mapView];
}

创建locationManager

if ([CLLocationManager locationServicesEnabled] == YES) {//判断定位是否可用
    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;//定位精确度
    _locationManager.distanceFilter = 10; //位置变化10米更新位置信息
    //NSLog(@"定位");
   
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [_locationManager requestWhenInUseAuthorization];
}else{
    //NSLog(@"定位不可用");
}

 [_locationManager startUpdatingLocation];

CLLocationManager Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    //NSLog(@"%s",__FUNCTION__);
    [manager stopUpdatingLocation];
    NSLog(@"newLocation = %@",newLocation);
    NSLog(@"oldLocation = %@",oldLocation);
    //获取经度和纬度的结构体
    CLLocationCoordinate2D coordinate = newLocation.coordinate;
    //纬度信息,CLLocationDegrees类型实际上就是double型
    CLLocationDegrees latitude = coordinate.latitude;
    //经度信息,CLLocationDegrees类型实际上就是double型
    CLLocationDegrees longitude = coordinate.longitude;
    NSLog(@"%f,%f",latitude,longitude);
    
    [self.latitude setText:[NSString stringWithFormat:@"纬度:%lf",latitude]];
    [self.longitude setText:[NSString stringWithFormat:@"经度:%lf",longitude]];
  /*
    
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             //详细信息
             [self.address setText:[NSString stringWithFormat:@"地址:%@",placemark.name]];
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
             //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
             [self.city setText:[NSString stringWithFormat:@"城市:%@",city]];
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    */
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败  ,%@",error);
}

MapView获得详细地址

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    
    CLLocation * newLocation = userLocation.location;
    self.lastCoordinate=mapView.userLocation.location.coordinate;
    
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    
    [standard setObject:@(self.lastCoordinate.longitude) forKey:MMLastLongitude];
    [standard setObject:@(self.lastCoordinate.latitude) forKey:MMLastLatitude];
    
    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
    {
        for (CLPlacemark * placeMark in placemarks)
        {
            NSDictionary *addressDic=placeMark.addressDictionary;
            
            NSString *City=[addressDic objectForKey:@"City"];
//            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
//            NSString * adressName  = [NSString stringWithFormat:@"%@%@",subLocality,street];
            [self.city setText:[NSString stringWithFormat:@"城市:%@",City]];
            
             [self.address setText:[NSString stringWithFormat:@"地址:%@",placeMark.name]];
            [self stopLocation];
        }
        
    };
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
    
}

停止定位

-(void)stopLocation
{
    if (_mapView) {
        NSLog(@"关闭");
        _mapView.showsUserLocation = NO;
        [_mapView removeFromSuperview];
        _mapView = nil;
    }
}

  

 

来源: http://www.cnblogs.com/spaceID/p/4992167.html

免责声明:文章转载自《CoreLocation+MapKit系统定位(含坐标以及详细地址)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇常见的MIME类型linux下如何添加一个用户并且让用户获得root权限下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ios开发 静态库制作

IOS程序是由Objective-c语言构成,而是Objective-c语言中每一个类又分成 .h .m 文件。静态库可以将这些程序的类封装成一个.a文件,第三方应用程序只需要拿到这个.a文件和代码对应的.h文件即可使用静态库中封装的方法。总的来说IOS静态库适合去制作第三方提供的SDK,废话不多说了我们直接进正题。 制作静态库 代码如下 1 #i...

查询指定距离内的快递柜或者店铺

背景:我们在淘宝购物时,选择了某个地址,有时会提示可以选择放到附近的快递柜子,这种是如何实现的呢?用redis geo api可以简单的实现该功能 思路:1. 我们先将所有的快递柜子存到redis中,这些快递柜信息要包含经纬度             /** * * @param longitude 经度 * @param...

iOS https请求 NSURLSessionDataTask

// //  YKSHttpsRequest.m //  YKShareSdkDemo // //  Created by qingyun on 22/05/2017. //  Copyright © 2017 qingjoin. All rights reserved. // #import "YKSHttpsRequest.h" @implementa...

KVC

1、概念: KVC(Key-value coding):键值对编码,也就是我们可以通过变量的名称来读取或者修改它的值,而不需要调用明确的存取方法。这样就可以在运行时动态地访问和修改对象的属性。而不是在编译时确定。对于类里的私有属性,Objective-C是无法直接访问的,但是KVC是可以的。 作用: 取值和赋值(开发中基本不用) 获取对象私有变量的值.(...

UITableView总结

性能优化 /** * 每当有一个cell要进入视野范围内,就会调用一次 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"wine...

iOS开发OC基础:OC数组对象NSArray的常用方法

本文介绍了OC的数组对象的基本方法的使用: 因为OC的数组中存储的为对象类型,所以我们可以新建一个Person类,通过Person生成对象进行操作。 其中Person.h中的代码为: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #import @interface Person : NSObject {     NSStri...