LBS,即Location Based Services,基于位置服务,用于定位、导航等功能,比如地图应用、订外卖等的app就需要这个功能。

在这里我使用的是高德LBS开放平台,地址:http://lbs.amap.com/

进入网站,首先注册并认证为开发者,然后为你的每个APP申请一个key,其中安全码(Bundle Identifier)通过Xcode切换到General标签,查看Bundle Identifier。

使用第三方服务,我们可以使用自动配置,这里就要使用到Cocoapods。CocoaPods是一个用来帮助我们管理第三方依赖库的工具。它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程连接起来,供我们开发使用。使用CocoaPods的目的是让我们能自动化的、集中的、直观的管理第三方开源库。

如果需要升级ruby,使用homebrew安装:brew install ruby

另外一种方法升级ruby: sudo gem update --system

安装Cocoapods教程:

gem sources -l
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
sudo gem install -n /usr/local/bin cocoapods
pod setup

创建Podfile:

cd /project
touch Podfile

或者直接cd到工程目录,然后pod init ,在Podfile里直接添加也行。

编辑Podfile文件:

source 'https://github.com/CocoaPods/Specs.git'
platform:ios,'7.0' #手机系统
#pod 'AMap3DMap' #3D地图SDK
pod 'AMap2DMap' #2D地图SDK(2D和3D不能同时使用)
pod 'AMapSearch' #搜索服务SDK 以下是额外补充
#platform :ios
#pod 'Reachability', '~> 3.0.0'
#pod 'SBJson', '~> 4.0.0' #platform :ios, '7.0'
#pod 'AFNetworking', '~> 2.0'

标准格式:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'AlamofireDemo' do
pod 'Alamofire'
pod 'SwiftyJSON'
end

然后到工程目录,输入命令pod  install,如果出现-bash: pod: command not found,输入sudo gem install -n /usr/local/bin cocoapods即可解决。

如果如果pod update / pod install 卡在:Updating local specs repositories,

等待即可,或者可以使用 “pod install --verbose --no-repo-update” 进行下载。

更新:如果你又添加新的三方库,可以在Podfile文件中继续添加,然后再终端输入pod update,然后CocoaPods就会重新帮你导入.

然后通过打开xcworkspace文件打开安装有cocoapods的工程

以下是我Controller.m的核心代码,仅供参考:

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
#define APIKey @"你申请的key" @interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate>
{
MAMapView *_mapView;
AMapSearchAPI *_search;
CLLocation *_currentLocation;
UIButton *_locationButton;
} @end @implementation ViewController - (void)initControls
{
_locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
_locationButton.frame = CGRectMake(, CGRectGetHeight(_mapView.bounds)-,,);
_locationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
_locationButton.backgroundColor = [UIColor whiteColor];
_locationButton.layer.cornerRadius = ;
[_locationButton addTarget:self action:@selector(locateAction) forControlEvents:UIControlEventTouchUpInside];
[_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
[_mapView addSubview:_locationButton]; }
//搜索服务
- (void)initSearch
{
_search = [[AMapSearchAPI alloc]initWithSearchKey:APIKey Delegate:self];
} //修改用户定位模式
- (void)locateAction
{
if (_mapView.userTrackingMode != MAUserTrackingModeFollow) {
[_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
}
}
//地理编码搜索请求
- (void)reGeoAction
{
if(_currentLocation)
{
AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc]init];
request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
[_search AMapGeocodeSearch:request];
}
} - (void)searchRequest:(id)request didFailWithError:(NSError *)error
{
NSLog(@"request :%@, error :%@",request,error);
}
- (void)onReGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
NSLog(@"response :%@",response);
NSString *title = response.regeocode.addressComponent.city;
if (title.length == ) {
title = response.regeocode.addressComponent.province;
}
_mapView.userLocation.title = title;
_mapView.userLocation.subtitle = response.regeocode.formattedAddress;
} //用户代理方法
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
{
//修改定位按钮状态
if(mode == MAUserTrackingModeNone)
{
[_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
}
else{
[_locationButton setImage:[UIImage imageNamed:@"location_yes"] forState:UIControlStateNormal];
}
} //记录当前所在位置
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
NSLog(@"userLocation: %@",userLocation.location);
_currentLocation = [userLocation.location copy]; } - (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
//选中定位annotation的时候进行逆地理编码查询
if ([view.annotation isKindOfClass:[MAUserLocation class]])
{
[self reGeoAction];
}
} //初始化地图
- (void)initMapView
{
[MAMapServices sharedServices].apiKey = APIKey;
_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
//设置地图的代理和两个附件的位置
_mapView.delegate = self;
_mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, );
_mapView.scaleOrigin = CGPointMake(_mapView.scaleOrigin.x, );
[self.view addSubview:_mapView];
//打开用户定位
_mapView.showsUserLocation = YES; } - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initMapView];
[self initSearch];
[self initControls];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

运行结果:

如果你有更多的API接口想要实现,你可以去高德LBS开放平台去看看开发文档,可以作进一步深入和开发。

github地址:https://github.com/AbelSu131/HelloAmap

基于LBS平台的iOS开发的更多相关文章

  1. 基于 Koa平台Node.js开发的KoaHub.js的控制器,模型,帮助方法自动加载

    koahub-loader koahub-loader是基于 Koa平台Node.js开发的KoaHub.js的koahub-loader控制器,模型,帮助方法自动加载 koahub loader I ...

  2. 基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码

    最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易 ...

  3. 基于 Koa平台Node.js开发的KoaHub.js获取/设置会话功能代码

    koa-session2 Middleware for Koa2 to get/set session use with custom stores such as Redis or mongodb ...

  4. 基于 Koa平台Node.js开发的KoaHub.js的静态服务器重写和索引代码

    koa-static-server Static file serving middleware for koa with directory, rewrite and index support k ...

  5. 基于 Koa平台Node.js开发的KoaHub.js的模板引擎代码

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  6. 基于 Koa平台Node.js开发的KoaHub.js的输出json到页面代码

    koahub-body-res koahub body res Format koa's respond json. Installation $ npm install koahub-body-re ...

  7. 基于 Koa平台Node.js开发的KoaHub.js的跳过组件代码

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  8. iOS开发---集成百度地图完善版

    一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...

  9. 《iOS开发指南》正式出版-源码-样章-目录,欢迎大家提出宝贵意见

    智捷iOS课堂-关东升老师最新作品:<iOS开发指南-从0基础到AppStore上线>正式出版了 iOS架构设计.iOS性能优化.iOS测试驱动.iOS调试.iOS团队协作版本控制.... ...

随机推荐

  1. bzoj1758Wc10重建计划——solution

    1758: [Wc2010]重建计划 Time Limit: 40 Sec  Memory Limit: 162 MBSubmit: 4707  Solved: 1200[Submit][Status ...

  2. js-ES6学习笔记-Class(6)

    1.类相当于实例的原型,所有在类中定义的方法,都会被实例继承.如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”. 2.父类的静态方法,可 ...

  3. CentOS7查看开放端口命令及开放端口号

    CentOS 7查看以开放端口命令:firewall-cmd —list-ports 查看端口是否开放命令:第一个方法就是使用lsof -i:端口号命令行,例如lsof -i:80.如果没有任何信息输 ...

  4. oop(Object Oriented Programming)

    嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...

  5. kafka-hadoop-consumer

    写了一个工具,从kafka传输数据到hdfs,采用的api,可以消费指定的kafka topic 或者为了简便可以消费所有的topic中各个partition的数据. 地址:https://githu ...

  6. Oracle 参数文件及相关操作介绍

    Oracle 参数文件及相关操作介绍 by:授客 QQ:1033553122 1.服务器参数文件 服务器参数文件是一个二进制文件,作为初始化参数的存储仓库.实例运行时,可用ALTER SYSTEM来改 ...

  7. Expo大作战(二十三)--expo中expo kit 高级属性(没干货)

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  8. 从零自学Java-1.编写第一个Java程序

    编写第一个Java程序 完成工作:1.在文本编辑器中输入一个Java程序. 2.使用括号组织程序. 3.保存.编译和运行程序. package com.Jsample;//将程序的包名称命名为com. ...

  9. 联想ThinkPadE455实现FN禁用(F1-F12标准功能与特殊功能切换)

    系统:Win7 64 位     机型:联想ThinkPadE455笔记本 方法一:键盘Fn热键切换功能(亲测可用) Fn+Esc   FnLk  组合键方法启用或禁用Fn锁定功能 具体说明(这个逻辑 ...

  10. python常用模块之string

    python常用模块string模块,该模块可以帮我们获取字母.数字.特殊符号. import string #打印所有的小写字母 print(string.ascii_lowercase) #打印所 ...