iOS开发---系统地图使用

第一步 : 设置定位服务请求  在plist文件中添加下面配置中至少一个:

1)NSLocationAlwaysAndWhenInUseUsageDescription

2)NSLocationWhenInUseUsageDescription

如果Xcode9.0以上版本请设置网络请求

第二步 :导入头文件

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>



第三步 : 设置代理和定义全局变量

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>

{

        MKMapView* _mapView;//地图视图

        CLLocationManager*_locationManager;//定位管理类

@end


第四步:实现代码

- (void)viewDidLoad {   

[super viewDidLoad]; 

  /**创建地图*/   

[self creatMap];   

/**定位当前*/ // 模拟器上无法定位   

[self selfLocation];

}

#pragma mark定位自己

-(void)selfLocation{   

//创建定位对象 

  _locationManager =[[CLLocationManager alloc] init];   

//设置定位属性   

_locationManager.desiredAccuracy =kCLLocationAccuracyBest; 

  //设置定位更新距离militer

    _locationManager.distanceFilter= 10.0; 

  //绑定定位委托 

  _locationManager.delegate = self;

    /**设置用户请求服务*/   

//1.去info.plist文件添加定位服务描述,设置的内容可以在显示在定位服务弹出的提示框    //取出当前应用的定位服务状态(枚举值)   

CLAuthorizationStatus status =[CLLocationManager authorizationStatus]; 

  //如果未授权则请求 

  if(status==kCLAuthorizationStatusNotDetermined) {   

    [_locationManager requestAlwaysAuthorization]; 

  }   

//开始定位   

[_locationManager startUpdatingLocation];

}


#pragma mark CLLocationManagerDelegate

//定位后的返回结果

-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{ 

  //locations数组中存储的是CLLocation 

  //遍历数组取出坐标--》如果不需要也可以不遍历   

CLLocation*location =[locations firstObject]; 

  //火星坐标转地球坐标   

//location=[location locationMarsFromEarth]; 

  //设置地图显示该经纬度的位置   

MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01,0.01)); 

  [_mapView setRegion:region animated:YES];   

//创建大头针 

MKPointAnnotation *   pointAnnotation = [[MKPointAnnotation alloc] init];   

//设置经纬度 

  pointAnnotation.coordinate = location.coordinate;   

//设置主标题   

pointAnnotation.title =@"The location of the I";   

//设置副标题 

  pointAnnotation.subtitle =@"Hello Map";   

//将大头针添加到地图上   

[_mapView addAnnotation:pointAnnotation];

}

#pragma mark创建地图

-(void)creatMap{ 

  //实例化 

  _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 

  // 是否可以缩放   

_mapView.zoomEnabled = YES;   

// 是否可以滚动   

_mapView.scrollEnabled = YES;

    // 是否可以旋转   

_mapView.rotateEnabled = YES;   

// 是否显示3D   

_mapView.pitchEnabled = YES; 

  // 是否显示指南针 

  _mapView.showsCompass = YES;   

// 是否显示比例尺   

_mapView.showsScale = YES; 

  // 是否显示交通   

_mapView.showsTraffic = YES; 

  // 是否显示建筑物 

  _mapView.showsBuildings = YES;   

//绑定委托   

_mapView.delegate=self; 

  //添加到界面   

[self.view addSubview:_mapView];   

//添加手势 

  UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 

  //添加到地图上   

[_mapView addGestureRecognizer:longPress]; 

  //创建UISegmentedControl 

  NSArray*mapTypeArray =@[@"标准",@"卫星地图",@"混合"]; 

  UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray]; 

  segment.frame=CGRectMake(50,50,300,50); 

  [_mapView addSubview:segment];   

segment.selectedSegmentIndex=0;   

//添加UISegmentedControl的事件响应方法 

  [segment addTarget:self action:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged];

}


//手势方法

-(void)longPress:(UILongPressGestureRecognizer *)sender{ 

  //判断是否是长按   

if(sender.state!=UIGestureRecognizerStateBegan) {   

    return; 

  }   

//获取手势在uiview上的位置 

  CGPoint longPressPoint = [sender locationInView:_mapView];   

//将手势在uiview上的位置转化为经纬度 

  CLLocationCoordinate2D  coordinate2d =[_mapView convertPoint:longPressPoint toCoordinateFromView:_mapView];   

NSLog(@"%f%f",coordinate2d.longitude,coordinate2d.latitude);

    //添加大头针   

MKPointAnnotation *pointAnnotation =[[MKPointAnnotation alloc] init]; 

  //设置经纬度 

  pointAnnotation.coordinate= coordinate2d;   

//设置主标题和副标题   

pointAnnotation.title=[NSString  stringWithFormat:@"纬度:%g,经度:%g",coordinate2d.latitude,coordinate2d.longitude]; 

  pointAnnotation.subtitle=@"你好,世界!"; 

  //添加到地图上 

  [_mapView addAnnotation:pointAnnotation]; 

  MKCircle*circle =[MKCircle  circleWithCenterCoordinate:coordinate2d radius:50];    //先添加,在回调方法中创建覆盖物 

  [_mapView addOverlay:circle];

}

//大头针的回调方法(与cell的复用机制很相似)

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{       

// ------- 自带的大头针-----

//   //复用

//    MKPinAnnotationView *annotationView =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];

//    //判断复用池中是否有可用的

//   if(annotationView==nil) {

//       annotationView =(MKPinAnnotationView *)[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];

//   }       

//   添加左边的视图  

 //    UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];

//    imageView.frame=CGRectMake(0,0,50,50);

//    annotationView.leftCalloutAccessoryView=imageView;

//     //显示

//    annotationView.canShowCallout=YES;

 //    //设置是否显示动画

//    annotationView.animatesDrop=YES;

 //    //设置右边视图

 //    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0,0,30,30)];

 //    label.text=@">>";

//    annotationView.rightCalloutAccessoryView=label;

 //    //设置大头针的颜色

//   annotationView.pinColor=MKPinAnnotationColorPurple;

 //    return annotationView;     

  /**  自定义大头针-------*/ 

  static NSString *pinId = @"pinID"; 

  MKAnnotationView *annoView = [mapView  dequeueReusableAnnotationViewWithIdentifier:pinId]; 

  if (annoView == nil) {       

annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation  reuseIdentifier:pinId]; 

  }   

annoView.annotation = annotation; 

  annoView.image = [UIImage imageNamed:@"2.jpg"];   

annoView.canShowCallout = YES;       

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];   

imageView.bounds = CGRectMake(0, 0, 44, 44);   

annoView.leftCalloutAccessoryView = imageView;    imageView.userInteractionEnabled  = YES;

        UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];  

  imageView2.bounds = CGRectMake(0, 0, 44, 44);

    annoView.rightCalloutAccessoryView = imageView2; 

  annoView.draggable = YES; 

  return annoView;  

 }

//覆盖物的回调方法

-(MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id)overlay{   

//创建圆形覆盖物   

MKCircleRenderer*circleRender =[[MKCircleRenderer alloc]initWithCircle:overlay];    //设置填充色   

circleRender.fillColor=[UIColor purpleColor];

    //设置边缘颜色 

  circleRender.strokeColor=[UIColor redColor]; 

  return circleRender;

}

//解决手势冲突,可以同时使用多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{   

return YES;

}

//segment响应回调

-(void)mapTypeChanged:(UISegmentedControl*)sender{   

//获得当前segment索引 

  NSInteger index = sender.selectedSegmentIndex;   

//设置地图的显示方式 

  _mapView.mapType= index;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容