类似美团的iOS地图定位及调用苹果系统导航
2016-03-31 15:22
615 查看
引入头文件
#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> #import "MapViewController.h" #import "AnnotationModel.h"
属性
@interface MapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate> @property (nonatomic, strong) CLLocationManager *manager; @property (nonatomic, strong) MKMapView *mapView; @property (nonatomic, strong) CLGeocoder *myGeocoder; @property (nonatomic, strong) MKAnnotationView *annotation; // 用于显示目的地详情,及导航按钮 @property (nonatomic, strong) UIView *viewLabel; // 导航按钮 @property (nonatomic, strong) UIButton *guidanceBtn; @property (nonatomic ,assign) CLLocationCoordinate2D ToCoords; @property (nonatomic ,assign) CLLocationCoordinate2D FromCoords;
初始化
_manager = [[CLLocationManager alloc]init]; _manager.delegate = self; [_manager requestAlwaysAuthorization]; [_manager startUpdatingLocation]; _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENHEIGHT)]; _mapView.delegate = self; _mapView.showsUserLocation = YES; _mapView.userTrackingMode = MKUserTrackingModeFollow; _viewLabel = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 80-64, SCREEN_WIDTH, 80)]; _viewLabel.backgroundColor = [UIColor whiteColor]; [_mapView addSubview:_viewLabel]; _guidanceBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 52, 5, 50, 30)]; _guidanceBtn.backgroundColor = [UIColor orangeColor]; [_guidanceBtn setTitle:@"导航" forState:UIControlStateNormal]; [_viewLabel addSubview:_guidanceBtn]; [_guidanceBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.mapView]; _mapView.delegate = self;
设置定位精确度
_manager.desiredAccuracy = kCLLocationAccuracyBest;
地理编码获取目的地经纬度
_myGeocoder = [[CLGeocoder alloc]init]; [self.myGeocoder geocodeAddressString:@"光谷国际广场" completionHandler:^(NSArray *placemarks,NSError *error){ if(nil == error && [placemarks count] > 0){ CLPlacemark *pm = [placemarks objectAtIndex:0]; CLLocationCoordinate2D coords= CLLocationCoordinate2DMake(pm.location.coordinate.latitude, pm.location.coordinate.longitude); self.ToCoords = coords; // 自定义的大头针 AnnotationModel *anno = [[AnnotationModel alloc]init]; anno.iconName = @"category_2"; anno.coordinate = CLLocationCoordinate2DMake(pm.location.coordinate.latitude, pm.location.coordinate.longitude); NSLog(@"%f,%f",anno.coordinate.latitude,anno.coordinate.longitude); // 地图显示精度 float zoomLevel = 0.01; MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel)); [_mapView setRegion:region animated:YES]; [_mapView addAnnotation:anno]; }else if([placemarks count] == 0 && error == nil){ NSLog(@"找不到给定地址的经纬度"); } }];
#pragma mark - CLLocationManager的代理方法
/** 用户拒绝或同意授权后调用*/ - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { NSLog(@"--%f",manager.location.coordinate.latitude); NSLog(@"--%f",manager.location.coordinate.longitude); self.FromCoords = manager.location.coordinate; [self.manager stopUpdatingLocation]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; _annotation = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"]; if (_annotation == nil) { _annotation = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"]; } _annotation.canShowCallout = YES; // 根据模型对象,决定本大头针显示的图片样式。 NSString *iconName = ((AnnotationModel *)annotation).iconName; _annotation.image = [UIImage imageNamed:iconName]; return _annotation;
}
调用苹果自带地图导航
- (void)btnClick { //当前的位置 MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; //起点 //MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]]; //目的地的位置 MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.ToCoords addressDictionary:nil]]; toLocation.name = @"目的地"; NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil]; NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES }; //打开苹果自身地图应用,并呈现特定的item [MKMapItem openMapsWithItems:items launchOptions:options];
}
AnnotationModel.h
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface AnnotationModel : NSObject <MKAnnotation> @property (nonatomic, copy) NSString *iconName; @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property(nonatomic, readonly, copy) NSString *title; @property(nonatomic, readonly, copy) NSString *subtitle; @end
AnnotationModel.m
#import "AnnotationModel.h" @implementation AnnotationModel @end
And more
相关文章推荐
- iOS创建与使用静态库
- ios: XcodeColors插件输出颜色不变
- iOS开发--cocoaPods使用详细说明(转载他人)
- IOS 中的 _cmd 代表本方法的名称
- iOS :button上如何去掉文字的下划线
- Nagios批量更换管理员
- iOS开发之代码块的使用
- iOS获取设备型号
- iOS开发入口类各方法功能汇总
- iOS横竖屏问题
- iOS-推送通知
- 正则表达式简单语法以及正则表达式在iOS中的常用方式
- iOS给项目设置启动页问题
- ios 浮点数精度问题
- 苹果开发者账号的种类
- iOS6、7、8、9新特性汇总和适配说明
- apns原生推送,收不到通知的问题解决
- ios 内存管理机制
- iOS开发技巧(系列十六:国际化Localization)
- ios 循环引用的场景