iOS地图定位

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

地图、定位定位:1、info.plist文件设置ios8以后,使用定位需要在info.plist文件中添加两个字段NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription2、导入CoreLocation.framework框架并导入头文件#importCoreLocation/CoreLocation.h3、判断定位服务是否打开if(![CLLocationManagerlocationServicesEnabled]){NSLog(@定位不可用);}4、创建定位管理器CLLocationManager*_manager=[[CLLocationManageralloc]init];5、判断是否授权,如果未授权则发送授权请求if([CLLocationManagerauthorizationStatus]==kCLAuthorizationStatusNotDetermined){[_managerrequestWhenInUseAuthorization];}6、设置代理(CLLocationManagerDelegate)_manager.delegate=self;7、设置精度_manager.desiredAccuracy=kCLLocationAccuracyBest;8、设置定位频率,多少米定位一次_manager.distanceFilter=10.0;9、开始定位[_managerstartUpdatingLocation];10、停止定位[_managerstopUpdatingLocation];11、代理方法-(void)locationManager:(CLLocationManager*)managerdidFailWithError:(NSError*)error{//定位失败}-(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations{CLLocation*location=[locationsfirstObject];CLLocationCoordinate2Dcoordinate=location.coordinate;NSLog(@经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f,coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);}地理编码,根据地址得出经纬度、详细信息等CLGeocoder*geocode=[[CLGeocoderalloc]init];[geocodegeocodeAddressString:@泰山completionHandler:^(NSArray*placemarks,NSError*error){CLPlacemark*place=[placemarksfirstObject];CLLocation*location=place.location;//位置CLRegion*region=place.region;//区域NSDictionary*dic=place.addressDictionary;//详细地址信息字典,包含以下字段NSString*name=place.name;//地名NSString*thoroughfare=place.thoroughfare;//街道NSString*subThoroughfare=place.subThoroughfare;//街道相关信息,例如门牌等NSString*locality=place.locality;//城市NSString*subLocality=place.subLocality;//城市相关信息,例如标志性建筑NSString*administrativeArea=place.administrativeArea;//州NSString*subAdministrativeArea=place.subAdministrativeArea;//其他行政区域信息NSString*postalCode=place.postalCode;//邮编NSString*ISOcountryCode=place.ISOcountryCode;//国家编码NSString*country=place.country;//国家NSString*inlandWater=place.inlandWater;//水源、湖泊NSString*ocean=place.ocean;//海洋NSArray*areasOfInterest=place.areasOfInterest;//关联的或利益相关的地标}];}反向地理编码,根据经纬度得出具体的地址信息CLLocation*location=[[CLLocationalloc]initWithLatitude:36.228longitude:117.042];CLGeocoder*geocoder=[[CLGeocoderalloc]init];[geocoderreverseGeocodeLocation:locationcompletionHandler:^(NSArray*placemarks,NSError*error){CLPlacemark*placemark=[placemarksfirstObject];NSLog(@详细信息:%@,placemark.addressDictionary);}];地图:1、导入MapKit.framework,并导入#importMapKit/MapKit.h头文件,实现MKMapViewDelegate协议2、创建_mapView=[[MKMapViewalloc]initWithFrame:self.view.bounds];3、设置代理_mapView.delegate=self;4、是否显示用户位置_mapView.showsUserLocation=YES;5、用户位置跟踪_mapView.userTrackingMode=MKUserTrackingModeFollow;//可以省略,但是需要自己设置地图的缩放级别6、代理方法:获取用户当前位置-(void)mapView:(MKMapView*)mapViewdidUpdateUserLocation:(MKUserLocation*)userLocation{MKCoordinateSpanspan=MKCoordinateSpanMake(0.01,0.01);MKCoordinateRegionregion=MKCoordinateRegionMake(userLocation.location.coordinate,span);[_mapViewsetRegion:regionanimated:true];//设置地图缩放级别}7、地图显示范围发生改变-(void)mapView:(MKMapView*)mapViewregionDidChangeAnimated:(BOOL)animated{NSLog(@地图显示范围发生改变);}8、添加大头针MKPointAnnotation*point=[[MKPointAnnotationalloc]init];//初始化point.title=@大头针;//标题point.subtitle=@我是大头针;//子标题point.coordinate=CLLocationCoordinate2DMake(36.236867,117.054895);//经纬度[_mapViewaddAnnotation:point];9、大头针被点击-(void)mapView:(MKMapView*)mapViewdidSelectAnnotationView:(MKAnnotationView*)view{NSLog(@大头针被点击);}10、自定义大头针视图-(MKAnnotationView*)mapView:(MKMapView*)mapViewviewForAnnotation:(idMKAnnotation)annotation{if([annotationisKindOfClass:[MKPointAnnotationclass]]){//判断是不是自己添加的大头针staticNSString*key1=@AnnotationKey1;MKAnnotationView*annotationView=[_mapViewdequeueReusableAnnotationViewWithIdentifier:key1];//获取大头针视图//如果缓存池中不存在则新建if(!annotationView){annotationView=[[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:key1];annotationView.canShowCallout=true;//允许交互点击annotationView.calloutOffset=CGPointMake(0,1);//定义详情视图偏移量UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];button.frame=CGRectMake(0,0,40,40);[buttonsetBackgroundImage:[UIImageimageNamed:@icon_classify_cafe.png]forState:UIControlStateNormal];annotationView.leftCalloutAccessoryView=button;//定义详情左侧视图}//修改大头针视图//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)annotationView.annotation=annotation;annotationView.image=[UIImageimageNamed:@icon_paopao_waterdrop_streetscape];//设置大头针视图的图片returnannotationView;}else{returnnil;}}11、大头针左侧或者右侧视图被点击-(void)mapView:(MKMapView*)mapViewannotationView:(MKAnnotationView*)viewcalloutAccessoryControlTapped:(UIControl*)control{if([controlisKindOfClass:[UIButtonclass]]){NSLog(@121212);}}12、调用系统自带地图进行导航-(void)turnByTurn{//根据“泰山学院”地理编码CLGeocoder*_geocoder=[[CLGeocoderalloc]init];[_geocodergeocodeAddressString:@泰山学院completionHandler:^(NSArray*placemarks,NSError*error){CLPlacemark*clPlacemark1=[placemarksfirstObject];//获取第一个地标MKPlacemark*mkPlacemark1=[[MKPlacemarkalloc]initWithPlacemark:clPlacemark1];//注意地理编码一次只能定位到一个位置,不能同时定位,所在放到第一个位置定位完成回调函数中再次定位[_geocodergeocodeAddre

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功