原生地图

1、什么是LBS

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

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

2、定位方式

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

3、框架

MapKit:地图框架,显示地图

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

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

MapKit:

  1. 初始化MapView

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

    [self.view addSubview:_mapView];

  2. 设置代理

_mapView.delegate = self;

  1. 设置地图类型

    _mapView.mapType = MKMapTypeStandard;

  2. 允许显示自己的位置

    _mapView.showsUserLocation = YES;

  3. 设置地图中心坐标点

    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(22.540396,113.951832);

    _mapView.centerCoordinate = centerCoordinate;

  4. 设置地图显示区域

a) 设置缩放

 MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

b) 设置区域

MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

c) 显示区域

_mapView.region = region;

CoreLocation:

  1. 初始化定位管理器

_manager = [[CLLocationManager alloc] init];

_manager.delegate = self;

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

  2. 在代码中加入

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

       [_manager requestAlwaysAuthorization];

    }

  1. 开启定位

    [_manager startUpdatingLocation];

  2. 定位成功代理

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

{

  NSLog(@"定位成功");

  //获取定位的坐标

  CLLocation *location = [locations firstObject];

  //获取坐标

  CLLocationCoordinate2D coordinate = location.coordinate;

  NSLog(@"定位的坐标:%f,%f", coordinate.longitude, coordinate.latitude);

  //停止定位

  //[_manager stopUpdatingLocation];

}

  1. 定位失败代理
  • (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

  NSLog(@"定位失败”);

}

  1. 屏幕坐标转经纬度坐标

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

  2. 反地理编码

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

//获取地标对象

      CLPlacemark *mark = [placemarks firstObject];

 }];

大头针(标注):

  1. 添加大头针

    //创建大头针

    MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];

    //设置坐标

    pointAnn.coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);

    //设置标题

    pointAnn.title = @"我的第一个大头针";

    //设置副标题

    pointAnn.subtitle = @"副标题";

    //显示大头针,把大头针加入到地图上

    [_mapView addAnnotation:pointAnn];

  2. 大头针的复用及定制

pragma mark - mapView 代理方法

//大头针View

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

{

//如果是自己当前位置的大头针,则不定制

if ( [annotation isKindOfClass:[MKUserLocation class]]) {

    return  nil;

}

if 1

// 1、自带的大头针视图

MKPinAnnotationView *pinAnnView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];

if ( !pinAnnView ) {

    pinAnnView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];

}

//设置大头针的颜色

pinAnnView.pinColor = MKPinAnnotationColorPurple;

//设置掉落动画

pinAnnView.animatesDrop = YES;

//是否弹出气泡

pinAnnView.canShowCallout = YES;

//设置左视图

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

leftView.backgroundColor = [UIColor blueColor];

pinAnnView.leftCalloutAccessoryView = leftView;

//设置右视图

UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

pinAnnView.rightCalloutAccessoryView = rightBtn;

return  pinAnnView;

else

//2、自定义大头针视图

/*

   * 区别于MKPinAnnotationView

   * 1、可以设置大头针图片

   * 2、不可以设置大头针颜色和掉落动画

   */

MKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];

if ( !customView ) {

    customView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];

}

//设置点击大头针可以显示气泡

customView.canShowCallout = YES;

//设置大头针图片

customView.image = [UIImage imageNamed:@"marker"];

//设置左视图

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

leftView.backgroundColor = [UIColor blueColor];

customView.leftCalloutAccessoryView = leftView;

//设置右视图

UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

customView.rightCalloutAccessoryView = rightBtn;

return  customView;

endif

}

  1. 移除大头针

    [_mapView removeAnnotations:_mapView.annotations];

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

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

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

[_mapView addGestureRecognizer:longPress];

pragma mark - 长按手势

-(void)longPress:(UILongPressGestureRecognizer *)gesture

{

//避免多次调用 只允许开始长按状态才添加大头针

if (gesture.state != UIGestureRecognizerStateBegan) {

    return;

}

//获取长按地图上的某个点

CGPoint point = [gesture locationInView:_mapView];

//把point转换成在地图上的坐标经纬度

CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];

//添加长按的大头针

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

annotation.coordinate = coordinate;

annotation.title = @"长按的大头针";

annotation.subtitle = @"副标题";

[_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:

  1. 配置高德地图API

define APIKEY @"e848d391f9c4b98db0935052777f99d2"

 [MAMapServices sharedServices].apiKey = APIKEY;
  1. 初始化MAMapView

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

 [self.view addSubview:_maMapView];
  1. 设置代理

    _maMapView.delegate = self;

  2. 设置地图类型

    _maMapView.mapType = MAMapTypeStandard;

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

    _maMapView.showsUserLocation = YES;

  4. 设置logo位置

    _maMapView.logoCenter = CGPointMake(100, 100);

  5. 显示罗盘

    _maMapView.showsCompass = YES;

  6. 显示交通

    _maMapView.showTraffic = YES;

  7. 是否支持旋转

    _maMapView.rotateEnabled = YES;

  8. 是否支持拖动

    _maMapView.scrollEnabled = YES;

  9. 是否支持缩放

_maMapView.zoomEnabled = NO;
  1. 设置地图显示区域

a) 设置坐标

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);

b) 设置缩放

MACoordinateSpan span = MACoordinateSpanMake(0.1, 0.1);

c) 设置区域

MACoordinateRegion region = MACoordinateRegionMake(coordinate, span);

d) 显示区域

_maMapView.region = region;
  1. 定位

pragma mark - 定位 回调方法

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

{

  NSLog(@"定位成功");

  CLLocation *location = userLocation.location;

  CLLocationCoordinate2D coordinate = location.coordinate;

  NSLog(@"我的坐标位置:%f, %f", coordinate.longitude, coordinate.latitude);

  // 定位后,可设置停止定位

  // _maMapView.showsUserLocation = NO;

}

  1. 添加标注(大头针)
//添加标注

  MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];

  annotation.coordinate = coordinate; //设置标注的坐标

  annotation.title = @"高德地图标题"; //设置标题

  annotation.subtitle = @"副标题"; //设置副标题

  [_maMapView addAnnotation:annotation]; //将标注添加在地图上
  1. 标注的复用及定制

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

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

{

  //不定制自己位置的标注视图

  if ( [annotation isKindOfClass:[MAUserLocation class]]) {

      return nil;

  }

if 1

  // 1、自带的标注视图

  MAPinAnnotationView *pinAnnView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];

  if ( !pinAnnView ) {

      pinAnnView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];

  }

  // 是否可弹出视图

  pinAnnView.canShowCallout = YES;

  // 设置掉落动画

  pinAnnView.animatesDrop = YES;

  // 设置标注颜色

  pinAnnView.pinColor = MAPinAnnotationColorGreen;

  // 设置左视图

  UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

  leftView.backgroundColor = [UIColor blueColor];

  pinAnnView.leftCalloutAccessoryView = leftView;

  //设置右视图

  UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

  pinAnnView.rightCalloutAccessoryView = rightBtn;

  return pinAnnView;

else

  //2、自定义标注视图

  MAAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];

  if ( !customView ) {

      customView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];

  }

  //设置点击大头针可以显示气泡

  customView.canShowCallout = YES;

  //设置大头针图片

  customView.image = [UIImage imageNamed:@"marker"];

  //设置左视图

  UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

  leftView.backgroundColor = [UIColor blueColor];

  customView.leftCalloutAccessoryView = leftView;

  //设置右视图

  UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

  customView.rightCalloutAccessoryView = rightBtn;

  return  customView;

endif

}

  1. 添加长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

    [_maMapView addGestureRecognizer:longPress];

pragma mark -- 长按手势Action

-(void)longPress:(UILongPressGestureRecognizer *)longPress

{

  if (longPress.state != UIGestureRecognizerStateBegan) {

      return;

  }

  //获取点位置

  CGPoint point = [longPress locationInView:_maMapView];

  //将点位置转换成经纬度坐标

  CLLocationCoordinate2D coordinate = [_maMapView convertPoint:point toCoordinateFromView:_maMapView];

  //在该点添加一个大头针(标注)

  MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];

  pointAnn.coordinate = coordinate;

  pointAnn.title = @"长按的大头针";

  pointAnn.subtitle = @"副标题";

  [_maMapView addAnnotation:pointAnn];

}

AMapSearchKit:

  1. 搜索周边

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

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

a) 创建搜索周边请求类

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

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

searchRequest.searchType = AMapSearchType_PlaceKeyword;

c) 设置关键字

searchRequest.keywords = keywordsTextField.text;

d) 设置搜索城市

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

e) 开始搜索

[searchAPI AMapPlaceSearch:searchRequest];
  1. 搜索周边回调方法

a) 搜索失败

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

{

NSLog(@"搜索失败");

}

b) 搜索成功

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

{

//清空原来的标注(大头针)

[_maMapView removeAnnotations:_maMapView.annotations];

//判断是否为空

if (response) {

    //取出搜索到的POI(POI:Point Of Interest)

    for (AMapPOI *poi in response.pois) {

        //poi的坐标

        CLLocationCoordinate2D coordinate =

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

        //地名

        NSString *name = poi.name;

        //地址

        NSString *address = poi.address;

        //用标注显示

        MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];

        pointAnn.coordinate = coordinate;

        pointAnn.title = name;

        pointAnn.subtitle = address;

        [_maMapView addAnnotation:pointAnn];

    }

}

}

  1. 添加折线

pragma mark - 画折线

-(void)drawPolyLine

{

//初始化点

NSArray *latitudePoints =[NSArray arrayWithObjects:

                          @"23.172223",

                          @"23.163385",

                          @"23.155411",

                          @"23.148765",

                          @"23.136935", nil];

NSArray *longitudePoints = [NSArray arrayWithObjects:

                            @"113.348665",

                            @"113.366056",

                            @"113.366128",

                            @"113.362391",

                            @"113.356785", nil];

// 创建数组

CLLocationCoordinate2D polyLineCoords[5];

for (int i=0; i<5; i++) {

    polyLineCoords[i].latitude = [latitudePoints[i] floatValue];

    polyLineCoords[i].longitude = [longitudePoints[i] floatValue];

}

// 创建折线对象

MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:polyLineCoords count:5];

// 在地图上显示折线

[_maMapView addOverlay:polyLine];

}

pragma mark - 定制折线视图

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

{

if ([overlay isKindOfClass:[MAPolyline class]]) {

    MAPolylineView *polyLineView = [[MAPolylineView alloc] initWithPolyline:overlay];

    polyLineView.lineWidth = 2; //折线宽度

    polyLineView.strokeColor = [UIColor blueColor]; //折线颜色

    polyLineView.lineJoinType = kMALineJoinRound; //折线连接类型

    return polyLineView;

}

return nil;

}

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

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

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

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

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

  3. iOS之原生地图与高德地图

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python excel操作总结

    1.openpyxl包的导入 Dos命令行输入 pip install openpyxl==2.3.3 这里注意一下openpyxl包的版本问题 版本装的太高有很多api不支持了,所以笔者这里用的是2 ...

  2. vbs文件小技巧

    vbs文件介绍: VBS是基于Visual Basic的脚本语言.VBS的全称是:Microsoft Visual Basic Script Editon.(微软公司可视化BASIC脚本版). 可以新 ...

  3. 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低

    1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...

  4. redux-form的学习笔记二--实现表单的同步验证

    (注:这篇博客参考自redux-form的官方英文文档)左转http://redux-form.com/6.5.0/examples/syncValidation/ 在这篇博客里,我将用redux-f ...

  5. 2764: [JLOI2011]基因补全

    2764: [JLOI2011]基因补全 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 570  Solved: 187[Submit][Status ...

  6. PowerDesigner建模应用(二)逆向工程,导出PDM文件前过滤元数据(表、视图、存储过程等)

    在上一篇文章<PowerDesigner建模应用(一)逆向工程,配置数据源并导出PDM文件>步骤二中导出了目标数据库对应的PDM文件, 该文件中展示出了所有表的信息与关系. 某些业务场景下 ...

  7. C++类对象大小的计算

    (一)常规类大小计算 C++类对象计算需要考虑很多东西,如成员变量大小,内存对齐,是否有虚函数,是否有虚继承等.接下来,我将对此举例说明. 以下内存测试环境为Win7+VS2012,操作系统为32位 ...

  8. MySQL数据库设计总结

    规则1:一般情况可以选择MyISAM存储引擎,如果需要事务支持必须使用InnoDB存储引擎. 注意:MyISAM存储引擎 B-tree索引有一个很大的限制:参与一个索引的所有字段的长度之和不能超过10 ...

  9. linux codeblocks汉化

    参考window汉化codeblocks,linux下有点麻烦,就是要找到codeblocks的文件安装在哪里.一般来说,linux下安装的软件大都在/usr/share/文件夹下,所以进入/usr/ ...

  10. BootStrap入门教程 (二)

    文本转自 http://www.cnblogs.com/ventlam/archive/2012/05/29/2520807.html 上讲回顾:Bootstrap的手脚架(Scaffolding)提 ...