ios地图总结

计算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 版本的开发指南。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容