iOS地图之MKMapView

使用MKMapView需要导入Mapkit框架

import

遵守协议

初始化定位管理器及地图
    _locationManager = [[CLLocationManager alloc] init];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [_locationManager requestAlwaysAuthorization];
        [_locationManager requestWhenInUseAuthorization];
    }
    
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.mapView];
    
    
    /*
     MKMapTypeStandard = 0,标准
     MKMapTypeSatellite,卫星
     MKMapTypeHybrid,鸟瞰
     MKMapTypeSatelliteFlyover 3D立体卫星
     MKMapTypeHybridFlyover 3D立体混合
     */
    
   
    self.mapView.mapType = MKMapTypeStandard;
     //默认都是YES
//    //设置滚动
//    self.mapView.scrollEnabled = NO;
//    //设置旋转
//    self.mapView.rotateEnabled = NO;
//    //设置缩放
//    self.mapView.zoomEnabled = NO;
   //比例尺
    self.mapView.showsScale = YES;
    //显示指南针
    self.mapView.showsCompass = YES;
    //显示交通
    self.mapView.showsTraffic = YES;
    //显示用户的位置,只是在地图上添加一个蓝色的店,标记当前位置
    //不会跟踪用户的位置信息
    self.mapView.showsUserLocation = YES;
    /*
     MKUserTrackingModeNone 不追踪
     MKUserTrackingModeFollow 追踪
     MKUserTrackingModeFollowWithHeading 追踪并显示方向
    */
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //设置代理
    self.mapView.delegate = self;
长按地图,出现当前位置信息
- (void)longPress{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.mapView addGestureRecognizer:longPress];
}



- (void)longPress:(UILongPressGestureRecognizer*)press{
    if (press.state == UIGestureRecognizerStateBegan) {
        //手指在mapView上的点
      CGPoint point = [press locationInView:self.mapView];
        //把点转换成为对应的经纬度
      CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        //初始化自定义大头针
        MyAnnotation *anno = [[MyAnnotation alloc] init];
        anno.coordinate = coordinate;
//        anno.title = @"标题";
//        anno.subtitle = @"副标题";
        
        //根据经纬度来生成一个CLLocation
        CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        //传入CLLocation和MyAnnotation进行反向编码并给大头针赋值
        [self reverseCode:location Annotation:anno];
        
        [self.mapView addAnnotation:anno];
        
    }
    
}

你可能感兴趣的:(iOS地图之MKMapView)