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 地图

    iOS 地图 基础 一 :定位 在 iOS 程序开发中 地图功能是普遍存在的,刚好最近都在研究地图的功能。总结一下...

  • ios地图总结

    计算2经纬度之间的距离(单位米) +(double)distanceBetweenOrderBy:(double)...

  • hbuilder ios 离线打包框架(适配iphonex)集成

    hbuilder ios 离线打包框架(适配iphonex)集成高德地图总结: 1.目前hbuilder内置的是百...

  • iOS 开发资料总结

    戴明大神的总结: 接下来,我按照 iOS 开发地图的顺序,和你推荐一些相关的学习资料。 实例 学习 iOS 开发最...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

  • iOS地图解析经纬度过程

    最近学习iOS地图比较多,所以做笔记记录一下,作为总结 对于地图和定位,苹果公司提供给了两个框架:MapKit:用...

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • iOS 使用高德地图正确姿势(三)

    iOS 使用高德地图正确姿势(一)iOS 使用高德地图正确姿势(二) 实现大头针始终在地图中心,拖动地图实时poi...

  • iOS原生地图 MKMapView 库翻译

    iOS原生地图 MKMapView 库翻译 作业部落存档 标签: iOS地图 引入系统库@import MapKi...

  • RN-地图导航

    调起百度网页地图路径导航 调起高德网页地图路径导航 iOS调起百度APP地图路径导航 iOS调起高德app地图路径...

网友评论

    本文标题:ios地图总结

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