美文网首页Ios@IONIC
类似美团的iOS地图定位及调用苹果系统导航

类似美团的iOS地图定位及调用苹果系统导航

作者: Chowie | 来源:发表于2016-03-31 15:38 被阅读1091次

    引入头文件

    #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地图定位及调用苹果系统导航

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