1:在iOS8中,定位采用了新的方法;
2:定位返回的是国际标准的经纬度,需要通过算法转变为中国坐标;
3:重新初始化CLLocation,并通过它得到位置的详细信息。
4:设置地图显示的流程:
(1)首先是添加定位功能,用以获得用户地理位置信息以及反编码的到的具体信息,例如:省市地区……
1> self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
self.locationManager.distanceFilter = 10;
[self.locationManager requestAlwaysAuthorization];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
2>CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *place = [placemarks firstObject];
NSLog(@"%@",place.name);
}];
(2)添加地图:首先设置地图显示的中心位置,设置完之后,就能显示地图了,并且是已自己为中心,拥有自己的放大倍数。
1>`#pragma mark 更新用户位置,只要用户改变则调用此方法(包括第一次定位到用户位置)
-(void)mapView:(MKMapView )mapView didUpdateUserLocation:(MKUserLocation )userLocation{
//设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点)
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);//地图放大的倍数
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
[_mapView setRegion:region animated:true];
}`
(3)设置大头针,可以自定义大头针的显示。
#pragma mark 显示大头针时调用,注意方法中的annotation参数是即将显示的大头针对象
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
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.annotation = annotation;
annotationView.image = [UIImage imageNamed:@"child_pin.png"];
return annotationView;
}
(4)如果要在地图上新添加要显示的大头针,可以使用MKAnnotation 的子类来设置。
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
#pragma mark 自定义一个图片属性在创建大头针视图时使用
@property (nonatomic,strong) UIImage *image;
@end
然后在.m 文件中实现,设置了location即可。