@PRoperty (weak, nonatomic) IBOutletMKMapView *mapView;
1 // 標(biāo)記用戶當(dāng)前位置2 // 跟蹤用戶位置3 [_mapView setUserTrackingMode:MKUserTrackingModeFollow];
// 地圖類型 [_mapView setMapType:MKMapTypeHybrid];
2.2.5.mapView:didUpdateUserLocation:當(dāng)用戶位置發(fā)生變化時(shí)調(diào)用
1 // 通過代理的方式可以跟蹤用戶的位置變化2 _mapView.delegate = self;
#pragma mark - 地圖代理方法#pragma mark 會(huì)頻繁調(diào)用,非常費(fèi)電- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ // 顯示用戶周邊信息 拉近地圖 設(shè)置地圖顯示區(qū)域
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 = @"重慶詳細(xì)描述";5 annotation2.imageName = @"head0";
[_mapView addAnnotation:annotation2];
1 #pragma mark 自定義大頭針視圖 2 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 3 { 4 // 判斷annotation參數(shù)是否是MyAnnotation 5 // 如果不是MyAnnotaion說明是系統(tǒng)的大頭針,無需做處理 6 if (![annotation isKindOfClass:[MyAnnotation class]]) { 7 // 使用系統(tǒng)默認(rèn)的大頭針 8 return nil; 9 }10 11 // 可重用標(biāo)示符12 static NSString *ID = @"MyAnno";13 14 // 查詢可重用的大頭針15 MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];16 17 // 如果沒有找到,再去實(shí)例化大頭針18 if (annoView == nil) {19 annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];20 21 // 自定義大頭針視圖,如果要接受用戶響應(yīng),需要設(shè)置此屬性22 annoView.canShowCallout = YES;23 }24 25 // 設(shè)置大頭針26 annoView.annotation = annotation;27 // 轉(zhuǎn)換成MyAnnotation28 // 設(shè)置大頭針視圖的圖像29 MyAnnotation *anno = annotation;30 annoView.image = [UIImage imageNamed:anno.imageName];31 32 NSLog(@"自定義大頭針");33 34 return annoView;35 }
// 定位服務(wù)管理器 CLLocationManager *_locationManager; // 使用地理編碼器 CLGeocoder *_geocoder;
locationServicesEnabled
1 2 if (![CLLocationManager locationServicesEnabled]) {3 NSLog(@"定位服務(wù)不可用!");4 return;5 }6
[_locationManager startUpdatingLocation];
reverseGeocodeLocation
1 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 2 { 3 NSLog(@"位置變化: %@", locations[0]); 4 5 // 根據(jù)經(jīng)緯度查找(去蘋果后臺(tái)查找準(zhǔn)確的位置,必須聯(lián)網(wǎng)才能用) 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
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注