地图

  1 #import "ViewController.h"
  2 #import <MapKit/MapKit.h>
  3 #import <CoreLocation/CoreLocation.h>
  4 
  5 @interface ViewController ()<MKMapViewDelegate>{
  6     MKMapView *mapview;
  7     CLGeocoder *geocoder;
  8     UITextField *jingdu;
  9     UITextField *weidu;
 10 }
 11 
 12 @end
 13 
 14 @implementation ViewController
 15 
 16 - (void)viewDidLoad {
 17     [super viewDidLoad];
 18     mapview = [[MKMapView alloc]initWithFrame:self.view.frame];
 19     [self.view addSubview:mapview];
 20     
 21     jingdu = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
 22     jingdu.backgroundColor=[UIColor redColor];
 23     jingdu.placeholder=@"23.126272";
 24     [self.view addSubview:jingdu];
 25     
 26     weidu = [[UITextField alloc]initWithFrame:CGRectMake(250, 100, 100, 30)];
 27     weidu.backgroundColor=[UIColor greenColor];
 28     weidu.placeholder=@"113.395568";
 29     [self.view addSubview:weidu];
 30     
 31     UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
 32     btn.frame=CGRectMake(135, 200, 100, 30);
 33     [btn setTitle:@"Go" forState:UIControlStateNormal];
 34     [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
 35     [self.view addSubview:btn];
 36     
 37     //初始化
 38     geocoder=[[CLGeocoder alloc]init];
 39     //设置地图的显示风格
 40     mapview.mapType=MKMapTypeStandard;
 41     //设置地图可缩放
 42     mapview.zoomEnabled=YES;
 43     mapview.scrollEnabled=YES;
 44     mapview.rotateEnabled=YES;
 45     mapview.showsUserLocation=YES;
 46     UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
 47     //为该控件添加手势处理器
 48     [self.view addGestureRecognizer:gesture];
 49     mapview.delegate=self;
 50 }
 51 -(void)btn:(id)sender{
 52     //关闭两个文本框的虚拟键盘
 53     [jingdu resignFirstResponder];
 54     [weidu resignFirstResponder];
 55     NSString *latitudeStr =jingdu.text;
 56     NSString *longtitudeStr = weidu.text;
 57     //用户输入的经度纬度不能为空
 58     if (latitudeStr!=nil&&latitudeStr.length>0 &&longtitudeStr !=nil && longtitudeStr.length>0) {
 59         //调用自己设置的方法设置地图的显示位置和现实区域
 60         [self locateToLatitudde:latitudeStr.floatValue longtitude:longtitudeStr.floatValue];
 61     }
 62 }
 63 -(void)locateToLatitudde:(CGFloat)latitude longtitude:(CGFloat)longitude{
 64     //设置地图中心的经度 纬度
 65     CLLocationCoordinate2D center = {latitude , longitude};
 66     //设置地图的显示范围
 67     MKCoordinateSpan span;
 68     //地图的显示范围越小,细节越清楚
 69     span.latitudeDelta=0.005;
 70     span.longitudeDelta=0.005;
 71     //创建MKCoordinateRegion对象,该对象代表了地图的显示中心和现实范围
 72     MKCoordinateRegion region = {center,span};
 73     //设置当前地图的显示中心和现实范围
 74     [mapview setRegion:region animated:YES];
 75     //创建MKPointAnnotation对象一个代表一个锚点
 76     MKPointAnnotation *anntation = [[MKPointAnnotation alloc]init];
 77     anntation.title=@"疯狂软件教育中心";
 78     anntation.title=@"广州天河区车坡大岗路4号大厦";
 79     CLLocationCoordinate2D coordinate= {latitude , longitude};
 80     anntation.coordinate=coordinate;
 81     //添加锚点
 82     [mapview addAnnotation:anntation];
 83 }
 84 -(void)longPress:(UILongPressGestureRecognizer *)gesture{
 85     //获取长按点的坐标
 86     CGPoint pos = [gesture locationInView:mapview];
 87     //将长按点的坐标转换为经纬度
 88     CLLocationCoordinate2D coord = [mapview convertPoint:pos toCoordinateFromView:mapview];
 89     //包装经纬度
 90     CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];
 91     //反向解析
 92     [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks , NSError *error){
 93         if (placemarks.count >0 && error==nil) {
 94             //获取解析的第一个地址
 95             CLPlacemark *placemark = [placemarks objectAtIndex:0];
 96             NSArray *addrArray = placemark.addressDictionary[@"FormattedAddressLines"];
 97             //将详细地址拼接成一个字符串
 98             NSMutableString *address = [[NSMutableString alloc]init];
 99             for (int i=0; i<addrArray.count; i++) {
100                 [address appendString:addrArray[i]];
101             }
102             MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
103             annotation.title=placemark.name;
104             annotation.subtitle=address;
105             annotation.coordinate=coord;
106             //添加锚点
107             [mapview addAnnotation:annotation];
108         }
109     }];
110 }
111 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
112     static NSString *annId =@"dddd";
113     MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annId];
114     //如果可以重用的控点不在
115     if (!annoView) {
116         annoView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annId];
117     }
118     //为控点设置图片
119     annoView.image=[UIImage imageNamed:@"a1c.png"];
120     annoView.canShowCallout=YES;
121     UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
122     [btn addTarget:self action:@selector(btn2:) forControlEvents:UIControlEventTouchUpInside];
123     annoView.rightCalloutAccessoryView=btn;
124     return annoView;
125 }
126 
127 -(void)btn2:(id)sender{
128     NSLog(@"点击锚点");
129 }

 

posted @ 2016-06-01 11:00  IOSsssssssss  阅读(98)  评论(0)    收藏  举报