
@PRoperty (weak, nonatomic) IBOutletMKMapView *mapView;
1 // 标记用户当前位置 2 // 跟踪用户位置 3 [_mapView setUserTrackingMode:MKUserTrackingModeFollow];
// 地图类型
[_mapView setMapType:MKMapTypeHybrid];
2.2.1.mapViewWillStartLoadingMap: 当地图界面将要加载时调用
2.2.2.mapView:viewForAnnotation: 当地图上有大头针时调用
2.2.3.mapViewWillStartLocatingUser:当准备进行一个位置定位时调用
2.2.4.mapView:regionDidChangeAnimated: 当显示的区域发生变化时调用
2.2.5.mapView:didUpdateUserLocation:当用户位置发生变化时调用
1 // 通过代理的方式可以跟踪用户的位置变化 2 _mapView.delegate = self;
#pragma mark - 地图代理方法
#pragma mark 会频繁调用,非常费电
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 显示用户周边信息 拉近地图 设置地图显示区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 100.0, 100.0);
[mapView setRegion:region animated:YES];
}
1 MyAnnotation *annotation2 = [[MyAnnotation alloc] init]; 2 annotation2.coordinate = CLLocationCoordinate2DMake(30, 106); 3 annotation2.title = @"重庆"; 4 annotation2.subtitle = @"重庆详细描述"; 5 annotation2.imageName = @"head0";
[_mapView addAnnotation:annotation2];
1 #pragma mark 自定义大头针视图
2 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
3 {
4 // 判断annotation参数是否是MyAnnotation
5 // 如果不是MyAnnotaion说明是系统的大头针,无需做处理
6 if (![annotation isKindOfClass:[MyAnnotation class]]) {
7 // 使用系统默认的大头针
8 return nil;
9 }
10
11 // 可重用标示符
12 static NSString *ID = @"MyAnno";
13
14 // 查询可重用的大头针
15 MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
16
17 // 如果没有找到,再去实例化大头针
18 if (annoView == nil) {
19 annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
20
21 // 自定义大头针视图,如果要接受用户响应,需要设置此属性
22 annoView.canShowCallout = YES;
23 }
24
25 // 设置大头针
26 annoView.annotation = annotation;
27 // 转换成MyAnnotation
28 // 设置大头针视图的图像
29 MyAnnotation *anno = annotation;
30 annoView.image = [UIImage imageNamed:anno.imageName];
31
32 NSLog(@"自定义大头针");
33
34 return annoView;
35 }
// 定位服务管理器
CLLocationManager *_locationManager;
// 使用地理编码器
CLGeocoder *_geocoder;
locationServicesEnabled
1
2 if (![CLLocationManager locationServicesEnabled]) {
3 NSLog(@"定位服务不可用!");
4 return;
5 }
6
[_locationManager startUpdatingLocation];
reverseGeocodeLocation
1 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
2 {
3 NSLog(@"位置变化: %@", locations[0]);
4
5 // 根据经纬度查找(去苹果后台查找准确的位置,必须联网才能用)
6 [_geocoder reverseGeocodeLocation:locations[0] completionHandler:^(NSArray *placemarks, NSError *error) {
7
8 NSLog(@"%@", placemarks[0]);
9
10 }];
11 }
geocodeAddressString
1 [_geocoder geocodeAddressString:@"王府井" completionHandler:^(NSArray *placemarks, NSError *error) {
2
3 for (CLPlacemark *placemark in placemarks) {
4 NSLog(@"aaaa______%@ %lu", placemark, (unsigned long)placemarks.count);
5 }
6
7 }];
清澈Saup