地理编码(根据地址得出位置并且加上自定义的大头针)
2015-08-28 09:36
741 查看
添加下面两句话在support.plist中
下面是主界面,根据地名获得经纬度
下面是自定义“大头针”类的.h文件
自定义“大头针”类.m文件
没有添加任何代码,这里不再赘述。
NSLocationWhenInUseUsageDescription YES; NSLocationAlwaysUsageDescription YES;
下面是主界面,根据地名获得经纬度
// // MapViewController.m // 地图test // // Created by administrator on 15/8/27. // Copyright (c) 2015年 gengcong. All rights reserved. // #import "MapViewController.h" #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> #import "Customannotation.h" @interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate> { CLLocationManager *_locationmanger; MKMapView *_mapView; CLGeocoder *_geocoder; double _latitude; double _longitude; } @end @implementation MapViewController - (void)viewDidLoad { [super viewDidLoad]; _geocoder =[[CLGeocoder alloc]init]; _mapView =[[MKMapView alloc]init]; _mapView.frame=self.view.frame; [self.view addSubview:_mapView]; // 设置代理 _mapView.delegate=self; _mapView.mapType=MKMapTypeStandard; [self getCoordinateByAddress:self.aaa]; // Do any additional setup after loading the view. } //longitude经度 //laititude纬度 -(void)addannotation:(double)longitude latitude:(double)latitude address:(NSString*)address { CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(latitude, longitude); Customannotation *annotation1=[[Customannotation alloc]init]; annotation1.title=address; annotation1.subtitle=[NSString stringWithFormat:@"上有天堂,下有%@",address]; annotation1.coordinate=location1; annotation1.image = [UIImage imageNamed:@"marker"]; [_mapView addAnnotation:annotation1]; [_mapView setRegion:MKCoordinateRegionMake(annotation1.coordinate, //移动地图到一个区域,这个区域的中心点annotation2,比例尺(.03, .03) MKCoordinateSpanMake(.03, .03)) animated:YES]; } -(void)getCoordinateByAddress:(NSString *)address { [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark=[placemarks firstObject]; //定义一个location存放 CLLocation *location=placemark.location;//位置 // CLRegion *region=placemark.region;//区域 // 获取经纬度 _latitude=location.coordinate.latitude; _longitude=location.coordinate.longitude; // NSDictionary *addressDic=placemark.addressDictionary; //添加大头针 [self addannotation:_longitude latitude:_latitude address:address]; }]; } #pragma mark - 地图控件代理方法 #pragma mark 显示大头针时调用,注意方法中的annotation参数是即将显示的大头针对象 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ //由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图 if ([annotation isKindOfClass:[Customannotation class]]) { static NSString *key1=@"AnnotationKey1"; //创建标注视图 MKAnnotationView *annotationView=[_mapView dequeueReusableAnnotationViewWithIdentifier:key1]; //如果缓存池中不存在则新建 if (!annotationView) { annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1]; annotationView.canShowCallout=true;//允许交互点击 annotationView.calloutOffset=CGPointMake(0, 1);//定义详情视图偏移量 annotationView.leftCalloutAccessoryView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cafe.png"]];//定义详情左侧视图 } //修改大头针视图 //重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置) annotationView.annotation=annotation; annotationView.image=((Customannotation *)annotation).image;//设置大头针视图的图片 return annotationView; }else { return nil; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
下面是自定义“大头针”类的.h文件
// // Customannotation.h // 地图test // // Created by administrator on 15/8/27. // Copyright (c) 2015年 gengcong. All rights reserved. // #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface Customannotation : NSObject<MKAnnotation> @property(nonatomic)CLLocationCoordinate2D coordinate; @property(nonatomic,copy) NSString *title; @property(nonatomic,copy) NSString *subtitle; @property(nonatomic,strong)UIImage *image; @end
自定义“大头针”类.m文件
没有添加任何代码,这里不再赘述。
相关文章推荐
- JAVA环境变量配置备忘
- 安卓解析 json 4种格式 全解析
- 初学Oracle UNPIVOT,PIVOT,WMSYS.WM_CONCAT三个函数
- spark官方文档翻译收藏
- iPhone开发之UIScrollView滚动组件的使用(三) 拖线实现按钮控制大图移动之contentInsets增加内容边距
- POJ 2578 && HDU 1037 Keep on Truckin'(水~)
- 天声人語 20150828
- 【SCOI2005】【BZOJ1087】互不侵犯King
- 【来日复制粘贴】状态栏随机显示
- java网页错误
- 关于iis7短日期后面带有星期的解决办法
- 扫盲知识点
- 代码规范
- 【排序算法】选择排序
- select 实现分析 –2 【整理】
- POJ 2575 Jolly Jumpers(水~)
- poj 2031(prim)
- 让jquery弹出窗口不被阻挡
- ubuntu虚拟机配置
- ubuntu 下apt-get update 遇到Encountered a section with no Package: header