iOS地图之MKMapView

使用MKMapView需要导入Mapkit框架

import <MapKit/MapKit.h>

遵守协议
<MKMapViewDelegate>

初始化定位管理器及地图
    _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];
        
    }
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,320评论 1 2
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,176评论 4 19
  • 显示地图 首先我们需要导入我们需要的框架 如图所示 之后导入框架的头文件 #import MKMapView 视图...
    烈酒暖心i阅读 2,326评论 0 3
  • 我不懂为什么我的颜色越来越淡 原本火烈烈的红逐渐变浅、变浅 最后如泡沫一样在空中消失 幻化为透明的我 漂浮在空中 ...
    然叔秀妹阅读 212评论 0 0
  • 早上,阳光透过宾馆廉价的窗帘,照在我脸上。 我迷迷糊糊醒来,翻身去拿床头的手机,突然僵住了。 一个女孩静静地蹲在我...
    简的一天阅读 396评论 0 0