IOS开发篇--地图的使用(一)

作者: 黑夜no烟丝 | 来源:发表于2016-05-18 09:10 被阅读5925次

地图是ios开发中常用到的功能,本文介绍苹果原生api地图的使用方法,使用地图必定要进行网络连接,注意的是苹果在ios9以后引入了https,所以进行网络请求时可以使用下面方法:

第一步:在Info.plist中添加NSAppTransportSecurity类型Dictionary。
第二步:在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
接下来要做的是设置定位服务请求,在plist文件中天下下面配置中至少一个:

1)NSLocationAlwaysUsageDescription

2)NSLocationWhenInUseUsageDescription

上面两步完成了以后,我们就可以进行地图的开发了:

实现定位要导入地图头文件,本例中使用原生地图,所以导入<Map.Kit/MapKit.h>后就可以创建地图了,如要获取指定经纬度或者实时定位自己的位置则还要导入核心定位服务头文件<CoreLocation/CoreLocation.h>,在这里我做了一个简单的Demo用以实现定位,值得一提的是苹果使用的是高德地图,其定位经纬度与地图定位度虐有偏差(具体原因你懂得),如果要实现准备定位,还要进行火星坐标(天朝)转地球坐标的运算(这个有人已经帮我们做好了,需要的可以上百度上去下,实在找不到我可以给你分享地址),以下是代码的内容:


#import

@interfaceMapViewController : UIViewController

@end


//MapViewController.m

//MapTest

//

//Created by on 16/5/15.

//Copyright © 2016年mobiletrain. All rights reserved.

//

#import"MapViewController.h"

/**核心定位服务头文件*/

#import <CoreLocation/CoreLocation.h>

/**原生地图头文件*/

#import<MapKit/MapKit.h>

/**火星坐标转换头文件*/

//#import"CLLocation+Sino.h"

@interfaceMapViewController ()

@end

@implementationMapViewController{

MKMapView * _mapView;//地图视图

CLLocationManager *_locationManager;//定位管理类

}

- (void)viewDidLoad {

[superviewDidLoad];

/**创建地图*/

[self creatMap];

/**定位当前*/

[selfselfLocation];

}

#pragma mark定位自己

-(void)selfLocation{

//创建定位对象

_locationManager =[[CLLocationManager alloc] init];

//设置定位属性

_locationManager.desiredAccuracy =kCLLocationAccuracyBest;

//设置定位更新距离militer

_locationManager.distanceFilter=10.0;

//绑定定位委托

_locationManager.delegate=self;

/**设置用户请求服务*/

//1.去info.plist文件添加定位服务描述,设置的内容可以在显示在定位服务弹出的提示框

//取出当前应用的定位服务状态(枚举值)

CLAuthorizationStatus status =[CLLocationManager authorizationStatus];

//如果未授权则请求

if(status==kCLAuthorizationStatusNotDetermined) {

[_locationManager requestAlwaysAuthorization];

}

//开始定位

[_locationManager startUpdatingLocation];

}

#pragma mark CLLocationManagerDelegate

//定位后的返回结果

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

//locations数组中存储的是CLLocation

//遍历数组取出坐标--》如果不需要也可以不遍历

CLLocation *location =[locations firstObject];

//火星坐标转地球坐标

//location=[location locationMarsFromEarth];

//设置地图显示该经纬度的位置

MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01,0.01));

[_mapView setRegion:region animated:YES];

//创建大头针

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

//设置经纬度

pointAnnotation.coordinate = location.coordinate;

//设置主标题

pointAnnotation.title =@"我在这里";

//设置副标题

pointAnnotation.subtitle =@"hello world";

//将大头针添加到地图上

[_mapView addAnnotation:pointAnnotation];

}

#pragma mark创建地图

-(void)creatMap{

//实例化

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

//绑定委托

_mapView.delegate=self;

//添加到界面

[self.view addSubview:_mapView];

//添加手势

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

//添加到地图上

[_mapView addGestureRecognizer:longPress];

//创建UISegmentedControl

NSArray *mapTypeArray =@[@"标准",@"卫星",@"混合"];

UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray];

segment.frame=CGRectMake(50,50,300,50);

[_mapView addSubview:segment];

segment.selectedSegmentIndex=0;

//添加UISegmentedControl的事件响应方法

[segment addTarget:selfaction:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged];

}

//手势方法

-(void)longPress:(UILongPressGestureRecognizer *)sender{

//判断是否是长按

if(sender.state!=UIGestureRecognizerStateBegan) {

return;

}

//获取手势在uiview上的位置

CGPoint longPressPoint = [sender locationInView:_mapView];

//将手势在uiview上的位置转化为经纬度

CLLocationCoordinate2D coordinate2d =[_mapView convertPoint:longPressPoint toCoordinateFromView:_mapView];

NSLog(@"%f%f",coordinate2d.longitude,coordinate2d.latitude);

//添加大头针

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

//设置经纬度

pointAnnotation.coordinate=coordinate2d;

//设置主标题和副标题

pointAnnotation.title=@"我在这里";

pointAnnotation.subtitle=@"你好,世界!";

//添加到地图上

[_mapView addAnnotation:pointAnnotation];

MKCircle *circle =[MKCircle circleWithCenterCoordinate:coordinate2d radius:50];

//先添加,在回调方法中创建覆盖物

[_mapView addOverlay:circle];

}

//大头针的回调方法(与cell的复用机制很相似)

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

//复用

MKPinAnnotationView *annotationView =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];

//判断复用池中是否有可用的

if(annotationView==nil) {

annotationView =(MKPinAnnotationView *)[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];

}

//添加左边的视图

UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];

imageView.frame=CGRectMake(0,0,50,50);

annotationView.leftCalloutAccessoryView=imageView;

//显示

annotationView.canShowCallout=YES;

//设置是否显示动画

annotationView.animatesDrop=YES;

//设置右边视图

UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0,0,30,30)];

label.text=@">>";

annotationView.rightCalloutAccessoryView=label;

//设置大头针的颜色

annotationView.pinColor=MKPinAnnotationColorPurple;

returnannotationView;

}

//覆盖物的回调方法

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{

//创建圆形覆盖物

MKCircleRenderer *circleRender =[[MKCircleRenderer alloc] initWithCircle:overlay];

//设置填充色

circleRender.fillColor=[UIColor purpleColor];

//设置边缘颜色

circleRender.strokeColor=[UIColor redColor];

returncircleRender;

}

//解决手势冲突,可以同时使用多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

returnYES;

}

//segment响应回调

-(void)mapTypeChanged:(UISegmentedControl *)sender{

//获得当前segment索引

NSInteger index =sender.selectedSegmentIndex;

//设置地图的显示方式

_mapView.mapType =index;

}

@end

相关文章

  • IOS开发篇--地图的使用(一)

    地图是ios开发中常用到的功能,本文介绍苹果原生api地图的使用方法,使用地图必定要进行网络连接,注意的是苹果在i...

  • iOS 开发篇--地图的使用(三)

    上次学习了用高德地图SDK定位和周边热点搜索,本次介绍地图的路线规划和导航,实现了输入联想和正反地理编码 1.使用...

  • IOS开发篇--地图的使用(二)

    上回我们学会了用苹果原生地图的使用,但是在实际开发中我们往往都是使用第三方SDK,因为它们真的很强大而且使用起来也...

  • 开始用Swift开发iOS 10 - 15 使用地图

    继续上一盘开始用Swift开发iOS 10 - 14 基础动画,模糊效果和Unwind Segue,这一篇使用地图...

  • iOS开发之地图

    在iOS开发中,地图也是很多App都需要使用的功能。本文主要对iOS中的地图知识点进行介绍。需要说明的是地图看似很...

  • ios开发使用的第三方资料

    ios开发使用的第三方资料 自己搜索使用过的一些资料,仅供参考。 室内地图-室内三维地图引擎导航及制作|易景地图:...

  • KVC

    iOS 如何使用KVC iOS开发UI篇—Kvc简单介绍 iOS开发系列--Objective-C之KVC、KVO

  • iOS 高德地图开发详解

    iOS 高德地图开发详解

  • iOS开发地图系列篇(一)

    应用场景 现在很多社交、电商、团购应用都引入了地图和定位功能,像我们平时生活常用到的外卖软件基本上都有定位功能,这...

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

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

网友评论

  • 9f76ef2f7833:你好,为什么大头针的回调函数会被多次调用呢?然后就显示了多个大头针。。。
    黑夜no烟丝:@勇敢的女孩99 嗯嗯 是的 当初还没考虑到这点 多谢 指正
    9f76ef2f7833:@黑夜no烟丝 谢谢楼主,已经搞定了,是因为开始定位后就会不断的走回调,然后创建大头针是在回调里执行的,只要在回调里创建大头针之前判断下就可以了,如果已经存在大头针只需要改变位置就可以了,不用新创建;如果不存在大头针,才需要创建大头针,新增到视图
    黑夜no烟丝:@勇敢的女孩99 你出现了什么情况?一次点击 就会回调一次 多个大头针 可能是因为没有上一次的 没有清除 半年前写的了 ,细节可能不清楚 。
  • 素酥:我怎怎么还是有偏差的
  • 小凡凡520:火星坐标系?怎么还有这个名字?
    ibabyblue:@小凡凡520 天朝特色嘛!
  • 368e29bbff1c:您好,火星坐标(天朝)转地球坐标的运算给我分享地址吧。
    黑夜no烟丝:@hujuuuu http://pan.baidu.com/s/1jI51Awm 提取码egm6

本文标题:IOS开发篇--地图的使用(一)

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