原生地图

1、什么是LBS

LBS: 基于位置的服务 Location Based Service

实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App

2、定位方式

1.GPS定位 2.基站定位 3.WIFI定位

3、框架

MapKit:地图框架,显示地图

CoreLocation:定位框架,没有地图时也可以使用定位.

4、如何使用原生地图 和定位

MapKit:

1) 初始化MapView

  1. _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  2. [self.view addSubview:_mapView];

2) 设置代理

_mapView.delegate = self;

3) 设置地图类型

  1. _mapView.mapType = MKMapTypeStandard;

4) 允许显示自己的位置

  1. _mapView.showsUserLocation = YES;

5) 设置地图中心坐标点

  1. CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(22.540396,113.951832);
  2. _mapView.centerCoordinate = centerCoordinate;

6) 设置地图显示区域

a) 设置缩放

  1. MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

b) 设置区域

  1. MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

c) 显示区域

  1. _mapView.region = region;

CoreLocation:

7) 初始化定位管理器

_manager = [[CLLocationManager alloc] init];

_manager.delegate = self;

8) iOS8定位

  1. 在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription

  2. 在代码中加入

    if ( [UIDevice currentDevice].systemVersion.floatValue >= 8.0 ) {

    1. [_manager requestAlwaysAuthorization];

    }

9) 开启定位

  1. [_manager startUpdatingLocation];

10) 定位成功代理

  • (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )locations

{

  1. NSLog(@"定位成功");
  2. //获取定位的坐标
  3. CLLocation *location = [locations firstObject];
  4. //获取坐标
  5. CLLocationCoordinate2D coordinate = location.coordinate;
  6. NSLog(@"定位的坐标:%f,%f", coordinate.longitude, coordinate.latitude);
  7. //停止定位
  8. //[_manager stopUpdatingLocation];

}

11) 定位失败代理

  • (void)locationManager:(CLLocationManager )manager didFailWithError:(NSError )error

{

  1. NSLog(@"定位失败”);

}

12) 屏幕坐标转经纬度坐标

  1. CLLocationCoordinate2D cl2d = [_mapView convertPoint:point toCoordinateFromView:_mapView];

13) 反地理编码

  1. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  2. [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

//获取地标对象

  1. CLPlacemark *mark = [placemarks firstObject];
  2. }];

大头针(标注):

14) 添加大头针

  1. //创建大头针
  2. MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];
  3. //设置坐标
  4. pointAnn.coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);
  5. //设置标题
  6. pointAnn.title = @"我的第一个大头针";
  7. //设置副标题
  8. pointAnn.subtitle = @"副标题";
  9. //显示大头针,把大头针加入到地图上
  10. [_mapView addAnnotation:pointAnn];

15) 大头针的复用及定制

pragma mark - mapView 代理方法

//大头针View

-(MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation

{

  1. //如果是自己当前位置的大头针,则不定制
  2. if ( [annotation isKindOfClass:[MKUserLocation class]]) {
  3. return nil;
  4. }

if 1

  1. // 1、自带的大头针视图
  2. MKPinAnnotationView *pinAnnView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
  3. if ( !pinAnnView ) {
  4. pinAnnView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];
  5. }
  6. //设置大头针的颜色
  7. pinAnnView.pinColor = MKPinAnnotationColorPurple;
  8. //设置掉落动画
  9. pinAnnView.animatesDrop = YES;
  10. //是否弹出气泡
  11. pinAnnView.canShowCallout = YES;
  12. //设置左视图
  13. UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  14. leftView.backgroundColor = [UIColor blueColor];
  15. pinAnnView.leftCalloutAccessoryView = leftView;
  16. //设置右视图
  17. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  18. pinAnnView.rightCalloutAccessoryView = rightBtn;
  19. return pinAnnView;

else

  1. //2、自定义大头针视图
  2. /*
  3. * 区别于MKPinAnnotationView
  4. * 1、可以设置大头针图片
  5. * 2、不可以设置大头针颜色和掉落动画
  6. */
  7. MKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];
  8. if ( !customView ) {
  9. customView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];
  10. }
  11. //设置点击大头针可以显示气泡
  12. customView.canShowCallout = YES;
  13. //设置大头针图片
  14. customView.image = [UIImage imageNamed:@"marker"];
  15. //设置左视图
  16. UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  17. leftView.backgroundColor = [UIColor blueColor];
  18. customView.leftCalloutAccessoryView = leftView;
  19. //设置右视图
  20. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  21. customView.rightCalloutAccessoryView = rightBtn;
  22. return customView;

endif

}

16) 移除大头针

  1. [_mapView removeAnnotations:_mapView.annotations];

17) 添加长按手势,实现在地图的长按点添加一个大头针

//4、长按地图显示大头针

  1. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  2. [_mapView addGestureRecognizer:longPress];

pragma mark - 长按手势

-(void)longPress:(UILongPressGestureRecognizer *)gesture

{

  1. //避免多次调用 只允许开始长按状态才添加大头针
  2. if (gesture.state != UIGestureRecognizerStateBegan) {
  3. return;
  4. }
  5. //获取长按地图上的某个点
  6. CGPoint point = [gesture locationInView:_mapView];
  7. //把point转换成在地图上的坐标经纬度
  8. CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];
  9. //添加长按的大头针
  10. MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
  11. annotation.coordinate = coordinate;
  12. annotation.title = @"长按的大头针";
  13. annotation.subtitle = @"副标题";
  14. [_mapView addAnnotation:annotation];

}

高德地图

1、高德地图申请Appkey流程:

a) 在浏览器中打开网址:http://lbs.amap.com/api/ios-sdk/guide/verify/

b) 访问:http://lbs.amap.com/console/key/,使用高德开发者账号登陆

c) 2.在“KEY管理”页面点击上方的“获取key”按钮,依次输入应用名,选择绑定的服务为“iOS平台SDK”,输入Bundle Identifier(Bundle Identifier获取方式为Xcode->General->Identity)

2、高德地图配置工程流程:

a)下载高德地图iOS SDK

b)添加高德地图的库文件MAMapKit.framework

c)添加8个关联库QuartzCore, CoreLocation, SystemConfiguration, CoreTelephony, libz, OpenGLES, libstdc++6.09, Security(MAMapView)

d)添加AMap.bundle(MAMapKit.framework->Resources)

e) TARGETS-Build Settings-Other Linker Flags 中添加内容: -ObjC;

f)在代码中添加用户Key: [MAMapServices sharedServices].apiKey =@"您的key";

3、单独使用搜索服务包:

a)添加搜索库文件AMapSearchKit.framework

b)添加关联库SystemConfiguration, CoreTelephony, libz, libstdc++6.09

c)在代码中添加AMapSearchAPI *search = [[AMapSearchAPI alloc] initWithSearchKey: @"您的key" Delegate:self];

4、如何使用高德地图 和 搜索

MAMapKit:

0) 配置高德地图API

define APIKEY @"e848d391f9c4b98db0935052777f99d2"

  1. [MAMapServices sharedServices].apiKey = APIKEY;

1) 初始化MAMapView

_maMapView = [[MAMapView alloc] initWithFrame:self.view.bounds];

  1. [self.view addSubview:_maMapView];

2) 设置代理

  1. _maMapView.delegate = self;

3) 设置地图类型

  1. _maMapView.mapType = MAMapTypeStandard;

4) 允许显示自己的位置(如使用定位功能,则必须设置为YES)

  1. _maMapView.showsUserLocation = YES;

5) 设置logo位置

  1. _maMapView.logoCenter = CGPointMake(100, 100);

6) 显示罗盘

  1. _maMapView.showsCompass = YES;

7) 显示交通

  1. _maMapView.showTraffic = YES;

8) 是否支持旋转

  1. _maMapView.rotateEnabled = YES;

9) 是否支持拖动

  1. _maMapView.scrollEnabled = YES;

10) 是否支持缩放

  1. _maMapView.zoomEnabled = NO;

11) 设置地图显示区域

a) 设置坐标

  1. CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);

b) 设置缩放

  1. MACoordinateSpan span = MACoordinateSpanMake(0.1, 0.1);

c) 设置区域

  1. MACoordinateRegion region = MACoordinateRegionMake(coordinate, span);

d) 显示区域

  1. _maMapView.region = region;

12) 定位

#pragma mark - 定位 回调方法

-(void)mapView:(MAMapView )mapView didUpdateUserLocation:(MAUserLocation )userLocation updatingLocation:(BOOL)updatingLocation

{

  1. NSLog(@"定位成功");
  2. CLLocation *location = userLocation.location;
  3. CLLocationCoordinate2D coordinate = location.coordinate;
  4. NSLog(@"我的坐标位置:%f, %f", coordinate.longitude, coordinate.latitude);
  5. // 定位后,可设置停止定位
  6. // _maMapView.showsUserLocation = NO;

}

13) 添加标注(大头针)

  1. //添加标注
  2. MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
  3. annotation.coordinate = coordinate; //设置标注的坐标
  4. annotation.title = @"高德地图标题"; //设置标题
  5. annotation.subtitle = @"副标题"; //设置副标题
  6. [_maMapView addAnnotation:annotation]; //将标注添加在地图上

14) 标注的复用及定制

#pragma mark - 定制标注视图(和原生地图定制方式类似)

  • (MAAnnotationView )mapView:(MAMapView )mapView viewForAnnotation:(id)annotation

{

  1. //不定制自己位置的标注视图
  2. if ( [annotation isKindOfClass:[MAUserLocation class]]) {
  3. return nil;
  4. }

#if 1

  1. // 1、自带的标注视图
  2. MAPinAnnotationView *pinAnnView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
  3. if ( !pinAnnView ) {
  4. pinAnnView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];
  5. }
  6. // 是否可弹出视图
  7. pinAnnView.canShowCallout = YES;
  8. // 设置掉落动画
  9. pinAnnView.animatesDrop = YES;
  10. // 设置标注颜色
  11. pinAnnView.pinColor = MAPinAnnotationColorGreen;
  12. // 设置左视图
  13. UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  14. leftView.backgroundColor = [UIColor blueColor];
  15. pinAnnView.leftCalloutAccessoryView = leftView;
  16. //设置右视图
  17. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  18. pinAnnView.rightCalloutAccessoryView = rightBtn;
  19. return pinAnnView;

#else

  1. //2、自定义标注视图
  2. MAAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];
  3. if ( !customView ) {
  4. customView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];
  5. }
  6. //设置点击大头针可以显示气泡
  7. customView.canShowCallout = YES;
  8. //设置大头针图片
  9. customView.image = [UIImage imageNamed:@"marker"];
  10. //设置左视图
  11. UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  12. leftView.backgroundColor = [UIColor blueColor];
  13. customView.leftCalloutAccessoryView = leftView;
  14. //设置右视图
  15. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  16. customView.rightCalloutAccessoryView = rightBtn;
  17. return customView;

#endif

}

15) 添加长按手势

  1. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  2. [_maMapView addGestureRecognizer:longPress];

#pragma mark -- 长按手势Action

-(void)longPress:(UILongPressGestureRecognizer *)longPress

{

  1. if (longPress.state != UIGestureRecognizerStateBegan) {
  2. return;
  3. }
  4. //获取点位置
  5. CGPoint point = [longPress locationInView:_maMapView];
  6. //将点位置转换成经纬度坐标
  7. CLLocationCoordinate2D coordinate = [_maMapView convertPoint:point toCoordinateFromView:_maMapView];
  8. //在该点添加一个大头针(标注)
  9. MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];
  10. pointAnn.coordinate = coordinate;
  11. pointAnn.title = @"长按的大头针";
  12. pointAnn.subtitle = @"副标题";
  13. [_maMapView addAnnotation:pointAnn];

}

AMapSearchKit:

1) 搜索周边

0) 创建AMapSearchAPI对象,配置APPKEY,同时设置代理对象为self

  1. searchAPI = [[AMapSearchAPI alloc] initWithSearchKey:APIKEY Delegate:self];

a) 创建搜索周边请求类

  1. AMapPlaceSearchRequest *searchRequest = [[AMapPlaceSearchRequest alloc] init];

b) 设置搜索类型(按关键字搜索)

  1. searchRequest.searchType = AMapSearchType_PlaceKeyword;

c) 设置关键字

  1. searchRequest.keywords = keywordsTextField.text;

d) 设置搜索城市

  1. searchRequest.city = @[@"广州"];

e) 开始搜索

  1. [searchAPI AMapPlaceSearch:searchRequest];

2) 搜索周边回调方法

a) 搜索失败

  • (void)searchRequest:(id)request didFailWithError:(NSError *)error

{

  1. NSLog(@"搜索失败");

}

b) 搜索成功

-(void)onPlaceSearchDone:(AMapPlaceSearchRequest )request response:(AMapPlaceSearchResponse )response

{

  1. //清空原来的标注(大头针)
  2. [_maMapView removeAnnotations:_maMapView.annotations];
  3. //判断是否为空
  4. if (response) {
  5. //取出搜索到的POI(POI:Point Of Interest)
  6. for (AMapPOI *poi in response.pois) {
  7. //poi的坐标
  8. CLLocationCoordinate2D coordinate =

CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);

  1. //地名
  2. NSString *name = poi.name;
  3. //地址
  4. NSString *address = poi.address;
  5. //用标注显示
  6. MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];
  7. pointAnn.coordinate = coordinate;
  8. pointAnn.title = name;
  9. pointAnn.subtitle = address;
  10. [_maMapView addAnnotation:pointAnn];
  11. }
  12. }

}

3) 添加折线

pragma mark - 画折线

-(void)drawPolyLine

{

  1. //初始化点
  2. NSArray *latitudePoints =[NSArray arrayWithObjects:
  3. @"23.172223",
  4. @"23.163385",
  5. @"23.155411",
  6. @"23.148765",
  7. @"23.136935", nil];
  8. NSArray *longitudePoints = [NSArray arrayWithObjects:
  9. @"113.348665",
  10. @"113.366056",
  11. @"113.366128",
  12. @"113.362391",
  13. @"113.356785", nil];
  14. // 创建数组
  15. CLLocationCoordinate2D polyLineCoords[5];
  16. for (int i=0; i<5; i++) {
  17. polyLineCoords[i].latitude = [latitudePoints[i] floatValue];
  18. polyLineCoords[i].longitude = [longitudePoints[i] floatValue];
  19. }
  20. // 创建折线对象
  21. MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:polyLineCoords count:5];
  22. // 在地图上显示折线
  23. [_maMapView addOverlay:polyLine];

}

pragma mark - 定制折线视图

-(MAOverlayView )mapView:(MAMapView )mapView viewForOverlay:(id)overlay

{

  1. if ([overlay isKindOfClass:[MAPolyline class]]) {
  2. MAPolylineView *polyLineView = [[MAPolylineView alloc] initWithPolyline:overlay];
  3. polyLineView.lineWidth = 2; //折线宽度
  4. polyLineView.strokeColor = [UIColor blueColor]; //折线颜色
  5. polyLineView.lineJoinType = kMALineJoinRound; //折线连接类型
  6. return polyLineView;
  7. }
  8. return nil;

}

iOS之原生地图与高德地图的更多相关文章

  1. React Native填坑之旅 -- 使用iOS原生视图(高德地图)

    在开发React Native的App的时候,你会遇到很多情况是原生的视图组件已经开发好了的.有的是系统的SDK提供的,有的是第三方试图组件,总之你的APP可以直接使用的原生视图是很多的.React ...

  2. ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)

    前言 在 ios上边使用地图库的同学肯定遇到过这样的问题:吹出框只能设置title和subtitle和左右的view,不管是百度地图还是高德地图还是自带的 google地图,只提供了这四个属性,如果想 ...

  3. iOS打开百度地图、高德地图导航

    1.判断手机里是否已经安装了百度地图或者高德地图: BOOL hasBaiduMap = NO; BOOL hasGaodeMap = NO; if ([[UIApplication sharedAp ...

  4. arcgis api 3.x for js 入门开发系列十七在线天地图、百度地图、高德地图(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  5. 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core

    百度地图和高德地图坐标系的互相转换   GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...

  6. 在WPF中使用谷歌地图和高德地图

    原文:在WPF中使用谷歌地图和高德地图 在桌面软件开发中可能会遇到这样的需求:显示地图. 常用的地图API有Google Map和高德地图.二者都提供了各种平台的API. 为了方便集成,本文使用Jav ...

  7. IOS原生地图与高德地图

    原生地图 1.什么是LBS LBS: 基于位置的服务   Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位  ...

  8. iOS原生地图与高德地图的使用

    原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...

  9. iOS第三方地图-高德地图(导航sdk路径规划)

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

随机推荐

  1. vbox磁盘空间扩容

    前提:将虚拟机真正关机,不能在仅状态保存的场合做磁盘扩容. 步骤1.获取需要增加容量的映像的uuid 在vbox的安装目录下使用命令行:VBoxManage list hdds 得到结果如下: UUI ...

  2. 第三百零四天 how can I 坚持

    我以为我遇到了,却是痴心妄想啊.哪有那么好的事.其实也无所谓,淡定,却又有点不淡定了. 洗澡睡觉吧,明天还要上班呢. 应该摆脱这种状态. 什么都不想,放空.

  3. DataSnap与FireDAC三层

    相交资料: http://blog.csdn.net/shuaihj/article/details/6129131http://www.cnblogs.com/hnxxcxg/p/4007876.h ...

  4. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  5. STM32的GPIO使用的函数剖析

    转载http://blog.csdn.net/wuwuhuizheyisheng/article/details/8239599 STM32的GPIO总结 作者:JCY 该文是自己学习了一段STM32 ...

  6. UVaLive 6855 Banks (水题,暴力)

    题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...

  7. 关于三目运算符与if语句的效率与洛谷P2704题解

    题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...

  8. ThinkPHP3.1.3的单字母函数汇总

    A函数: 用于实例化Action 格式:[项目://][分组/]模块 /** * A函数用于实例化Action 格式:[项目://][分组/]模块 * @param string $name Acti ...

  9. jdbc线程池

    连接oracle数据库的jdbc线程池 首先建立一个properties类型的文件存放一些信息:jdbc.properties driverClassName=oracle.jdbc.driver.O ...

  10. opencv 手势识别

    我使用OpenCV2.4.4的windows版本+Qt4.8.3+VS2010的编译器做了一个手势识别的小程序. 本程序主要使到了Opencv的特征训练库和最基本的图像处理的知识,包括肤色检测等等. ...