地图操作原理

原生地图

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;

	3) 设置地图类型
	 _mapView.mapType = MKMapTypeStandard;

	4) 允许显示自己的位置
	 _mapView.showsUserLocation = YES;

		5) 设置地图中心坐标点
	 CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(22.540396,113.951832);
	 _mapView.centerCoordinate = centerCoordinate;

	6) 设置地图显示区域
		a) 设置缩放
	 	MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

		b) 设置区域
		MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

		c) 显示区域
		_mapView.region = region;

CoreLocation:
7) 初始化定位管理器
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;112

		8) iOS8定位
			1. 在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription
			2. 在代码中加入     
	#if  __IPHONE_8_0
		if ( [UIDevice currentDevice].systemVersion.floatValue >= 8.0 ) {
   		   [_manager requestAlwaysAuthorization];  
		  [_manager requestWhenInUseAuthorization];
	 	}
	#endif

	9) 开启定位
	[_manager startUpdatingLocation];

	10) 定位成功代理
		- (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];

		}

	11) 定位失败代理
		- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
		{
		  NSLog(@"定位失败”);
		}

		12) 屏幕坐标转经纬度坐标
     CLLocationCoordinate2D cl2d = [_mapView convertPoint:point toCoordinateFromView:_mapView];
			
		13) 反地理编码
	 CLGeocoder *geocoder = [[CLGeocoder alloc] init];
	 [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
		
		 //获取地标对象
    	  CLPlacemark *mark = [placemarks firstObject];
	 }];

大头针(标注):

		14) 添加大头针
		    //创建大头针
	   MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];

	   //设置坐标
	   pointAnn.coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);
	   
	   //设置标题
	   pointAnn.title = @"我的第一个大头针";

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

	   //显示大头针,把大头针加入到地图上
	   [_mapView addAnnotation:pointAnn];


		15) 大头针的复用及定制
		#pragma  mark - mapView 代理方法
		//大头针View
		-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)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
		
		}
		
				
		16) 移除大头针
	  [_mapView removeAnnotations:_mapView.annotations];
		 
		17)  添加长按手势,实现在地图的长按点添加一个大头针
		  //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];
			
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GOD FOR JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值