ios地图总结

作者: 代码干货 | 来源:发表于2015-04-18 10:07 被阅读7000次

    计算2经纬度之间的距离(单位米)

    <pre><code>
    +(double)distanceBetweenOrderBy:(double)lat1 :(double)lat2 :(double)lng1 :(double)lng2
    {
    CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    double distance = [curLocation distanceFromLocation:otherLocation];
    return distance;
    }
    </code></pre>

    获取当前手机所在位置

    注意事项:需要考虑适配的问题,ios8下定位会失败,需要做如下2步骤:
    1、在开启刷新[_locationManager startUpdatingLocation];前添加[_locationManager requestAlwaysAuthorization];(后台定位)或[_locationManager requestWhenInUseAuthorization](前台定位)
    2、在info.list中添加key :NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription

    详细的参考代码
    加入如下头文件
    CoreLocation/CoreLocation.h
    AddressBook/AddressBook.h
    <pre><code>

    import "SystemMapkitViewController.h"

    define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

    @interface SystemMapkitViewController ()<CLLocationManagerDelegate>

    @property(nonatomic, strong) CLLocationManager *locationManager;

    @end

    @implementation SystemMapkitViewController

    • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
      // Custom initialization
      }
      return self;
      }

    • (void)viewDidLoad
      {
      [super viewDidLoad];

      //定位服务管理对象初始化
      _locationManager = [[CLLocationManager alloc] init];
      _locationManager.delegate = self;
      //设置精度 精度越高请求获得位置信息的频率就越高,着就也为着设备越耗电
      _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      //设置移动更新位置的最小距离
      _locationManager.distanceFilter = 1000.0f;
      self.view.backgroundColor = [UIColor darkGrayColor];

    }

    -(void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];

    //ios8下定位服务时效 需要如下处理
    /*requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.
     There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.
     */
    if(IS_OS_8_OR_LATER) {
        [_locationManager requestAlwaysAuthorization];
    }
    //开启刷新
    [_locationManager startUpdatingLocation];
    

    }

    -(void)viewWillDisappear:(BOOL)animated
    {
    //关闭服务
    [_locationManager stopUpdatingLocation];
    }

    //当设备到达过滤距离时刷新
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    CLLocation *currentLocation = locations.lastObject;

    //地理信息反编码
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (placemarks > 0) {
            CLPlacemark *placemark = placemarks[0];
            NSDictionary *addressDictionary = placemark.addressDictionary;
            NSString *stree = [addressDictionary objectForKey:(NSString*)kABPersonAddressStreetKey]; //街道信息
            stree = stree = nil ? @"": stree;
            NSString *city = [addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey]; //城市
            city = city = nil ? @"": city;
            NSString *stateaddress = [addressDictionary objectForKey:(NSString*)kABPersonAddressStateKey]; //州,省
            stateaddress = stateaddress = nil ? @"": stateaddress;
            NSString *zip = [addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]; //zip
            zip = zip = nil ? @"": zip;
            NSString *country = [addressDictionary objectForKey:(NSString*)kABPersonAddressCountryKey]; //国家
            country = country = nil ? @"": country;
            NSString *CountryCode = [addressDictionary objectForKey:(NSString*)kABPersonAddressCountryCodeKey]; //
            CountryCode = CountryCode = nil ? @"": CountryCode;
            NSLog(@"%@\n%@\n%@\n%@\n%@\n%@",stree, city, stateaddress, zip, country,CountryCode);
            
        }
        
    }];
    
    NSString *latitude = [NSString stringWithFormat:@"%3.f",currentLocation.coordinate.latitude]; //维度
    NSString *longitude = [NSString stringWithFormat:@"3.5f",currentLocation.coordinate.longitude];//精度
    NSString *altitude = [NSString stringWithFormat:@"3.5f",currentLocation.altitude]; //高度
    NSLog(@"维度:%@\n精度:%@\n高度:%@\n",latitude,longitude, altitude);
    

    }

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

    • (void)didReceiveMemoryWarning
      {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      }

    @end

    </code></pre>

    IOS利用高德导航实现周边搜索

    在开始写之前先准备下如下几件事:
    第一件事:申请 高德导航Key
    注意:你的程序名称和bundle identifier必须和申请时注册的一致,不否会无法向下进行
    搜索库,点击下载。解压后得到AMapSearchKit.framework文件。
    添加相关库文件:
    libstdc.6++
    QuartzCore
    CoreLocation
    SystemCOnfiguration
    libz
    OpenGLES
    CoreTelePhony
    Security
    在 TARGETS->Build Settings->Other Linker Flags 中添加-ObjC
    POI搜索介绍:

    高德地图提供了千万级别的POI(Point of Interesting,兴趣点)。在地图表达中,一个POI可代表一栋大厦、一家商铺、一处景点等等。

    iOS SDK包括3种类型的POI搜索:关键字搜索、周边搜索、指定区域搜索。不同类型的POI搜索,区别在于构造的搜索参数。其中:
    关键字搜索:keywords(关键词)和 types(类型)必设其一,searchType 为 AMapSearchType_PlaceKeyword。
    周边搜索:keywords(关键词)和 types(类型)必设其一,还必设 location(中心点坐标),searchType为AMapSearchType_PlaceAround。
    指定区域搜索:keywords(关键词)和 types(类型)必设其一,还必设 polygon(多边形),searchType为AMapSearchType_PlacePolygon。
    说明:types为POI的类型,编码表下载地址: http://lbs.amap.com/wp-content/uploads/2014/06/AMap_Api_Table.zip

    详细代码如下:
    <pre><code>

    import "GaodeMapKitViewController.h"

    import <AMapSearchKit/AMapSearchAPI.h>

    @interface GaodeMapKitViewController ()<AMapSearchDelegate>
    {
    AMapSearchAPI * _search;
    }
    @end

    @implementation GaodeMapKitViewController

    • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
      // Custom initialization
      }
      return self;
      }

    • (void)viewDidLoad
      {
      [super viewDidLoad];

      //初始化检索对象
      _search = [[AMapSearchAPI alloc] initWithSearchKey:keyChain Delegate:self];

      //沟槽AMapPlaceSearchRequest 对象,配置关键字搜索参数
      AMapPlaceSearchRequest *poiRequest = [[AMapPlaceSearchRequest alloc] init];
      // poiRequest.searchType = AMapSearchType_PlaceKeyword;
      poiRequest.searchType = AMapSearchType_PlaceAround;
      // 查询关键字,多个关键字用“|”分割,“空格"表示与,“双引号”表示不可分割
      // poiRequest.keywords = @"服装";
      poiRequest.types = @[@"050102",@"050117",@"060411"];
      //注意: keywords和types必设其一,设置2个会冲突,什么也查不到

      poiRequest.city = @[@"beijing"];
      poiRequest.requireExtension = YES;
      // poiRequest.radius = 100; //查询半径,单位:米 [default = 3000]
      //设置中心点(史各庄)
      poiRequest.location = [AMapGeoPoint locationWithLatitude:40.10381112 longitude:116.29337311];
      //发起 POI 搜索
      [_search AMapPlaceSearch: poiRequest];
      }

    //实现 POI 搜索对应的回调函数

    • (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
      {
      if(response.pois.count == 0) {
      return; }
      //处理搜索结果的总记录数
      NSLog(@"记录数:%ld",response.count);

      for (AMapPOI *p in response.pois) {
      //显示搜索到的名字
      NSLog(@"%@",p.name);

      }

    }

    • (void)didReceiveMemoryWarning
      {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      }

    @end
    </code></pre>

    下面给出项目中用到的工具:
    高德导航在线获取经纬度
    下载 POI 分类编码表和城市编码表。

    学习高德地图需要的网址:
    高德参考API 说明:平常使用的定位,查找都有简单的例子
    下载高德地图 iOS V2.4.X 版本的开发指南。

    相关文章

      网友评论

        本文标题:ios地图总结

        本文链接:https://www.haomeiwen.com/subject/beiefttx.html