0.使用百度地圖,點(diǎn)擊屏幕下設(shè)置標(biāo)注,并顯示出當(dāng)前標(biāo)注的地區(qū)地址
1.項(xiàng)目工程文件設(shè)置參考百度SDK
2.在百度地圖顯示頁(yè)面
#pragma mark -懶加載
//地圖
- (BMKMapView *)mapView
{
if (!_mapView) {
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, -64, kWeight, kHeight)];
self.displayParam = [[BMKLocationViewDisplayParam alloc]init];
_displayParam.isRotateAngleValid = YES;//跟隨態(tài)旋轉(zhuǎn)角度是否生效
_displayParam.isAccuracyCircleShow = NO;//精度圈是否顯示
//_displayParam.locationViewImgName= @"icon";//定位圖標(biāo)名稱
_displayParam.locationViewOffsetX = 0;//定位偏移量(經(jīng)度)
_displayParam.locationViewOffsetY = 0;//定位偏移量(緯度)
[_mapView updateLocationViewWithParam:_displayParam];
}
return _mapView;
}
//地圖搜索
- (BMKGeoCodeSearch *)geocodesearch
{
if (!_geocodesearch) {
_geocodesearch = [[BMKGeoCodeSearch alloc] init];
_geocodesearch.delegate = self;
}
return _geocodesearch;
}
//地圖標(biāo)注
- (BMKPointAnnotation *)annotation
{
if (!_annotation) {
_annotation =[[BMKPointAnnotation alloc]init];
}
return _annotation;
}
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil,否則影響內(nèi)存的釋放
}
- (void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時(shí),置nil
}
#pragma mark -viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
//添加地圖View
[self.view addSubview:self.mapView];
//開啟定位服務(wù)
self.locService = [[BMKLocationService alloc] init];
self.locService.delegate = self;
//啟動(dòng)LocationService
[self.locService startUserLocationService];
//初始化地圖顯示位置 上海
[self setUpLocal];
//設(shè)定是否顯示定位圖層 及 定位精確度
_mapView.showsUserLocation = YES;//顯示定位圖層
_mapView.userTrackingMode = BMKUserTrackingModeNone;
//添加位置顯示框
[self addLocationLabel];
//添加確定按鈕
[self addDoneButton];
}
//初始化地圖顯示在上海
- (void)setUpLocal
{
BMKCoordinateRegion region ;//表示范圍的結(jié)構(gòu)體
CLLocationCoordinate2D coor;
coor.latitude = 31.171321;
coor.longitude = 121.411398;
region.center = coor;//中心點(diǎn)
region.span.latitudeDelta = 0.1;//經(jīng)度范圍(設(shè)置為0.1表示顯示范圍為0.2的緯度范圍)
region.span.longitudeDelta = 0.1;//緯度范圍
[_mapView setRegion:region animated:YES];
}
//添加位置顯示框
- (void)addLocationLabel
{
UILabel *locationLabel = [[UILabel alloc] init];
locationLabel.backgroundColor = [UIColor whiteColor];
locationLabel.text = @"寄件地址: ";
locationLabel.frame = CGRectMake(20, 25, AppWeight-40, 30);
locationLabel.numberOfLines = 0;
locationLabel.font = [UIFont systemFontOfSize:12];
// 把label放在最頂層
[self.view insertSubview:locationLabel aboveSubview:self.mapView];
self.locationLabel = locationLabel;
}
//添加確定按鈕
- (void)addDoneButton
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.doneButton = doneButton;
doneButton.frame = CGRectMake(30, AppHeight - 120, AppWeight-60, 45);
[doneButton setBackgroundColor:[UIColor orangeColor]];
[doneButton setTitle:@"確 定" forState:UIControlStateNormal];
[doneButton setTitle:@"確 定" forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
}
//完成按鈕 把當(dāng)前標(biāo)注的地址傳給上一個(gè)控制器的textField
- (void)doneButtonClick:(UIButton *)button
{
NSString *str = self.locationLabel.text;
str = [str substringFromIndex:6];
if (_localStringBlock) {
_localStringBlock(str);
}
[self.navigationController popViewControllerAnimated:YES];
}
# pragma mark -BMKMapViewDelegate
//點(diǎn)擊屏幕 添加標(biāo)注
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
{
//拿到點(diǎn)擊的經(jīng)緯度 然后添加標(biāo)
CLLocationCoordinate2D coor;
coor.latitude = coordinate.latitude;
coor.longitude = coordinate.longitude;
self.annotation.coordinate = coor;
[_mapView addAnnotation:self.annotation];
[self.mapView setCenterCoordinate:coor animated:YES];
///反geo檢索信息類
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeocodeSearchOption.reverseGeoPoint = coor;
BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];;
if (flag) {
NSLog(@"反地理編碼成功");
}else {
NSLog(@"反地理編碼失敗");
}
}
//根據(jù)anntation生成對(duì)應(yīng)的view
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
NSString *AnnotationViewID = @"renameMark";
BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
annotationView.image = [UIImage imageNamed:@"地圖-選中"];
}
return annotationView;
}
#pragma mark -BMKGeoCodeSearchDelegate
//根據(jù) 經(jīng)緯度 獲取 地區(qū)信息
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
// NSLog(@"%@",result.address);
// NSLog(@"%@",result.addressDetail);
self.locationLabel.text = [NSString stringWithFormat:@"寄件地址: %@",result.address];
}else {
NSLog(@"未找到結(jié)果");
}
}
/**
* 當(dāng)選中一個(gè)annotation views時(shí)讲婚,調(diào)用此接口
* @param mapView 地圖View
* @param views 選中的annotation views
*/
//- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
//{
// _shopCoor = view.annotation.coordinate;
//}
///**
// * 選中氣泡調(diào)用方法
// * @param mapView 地圖
// * @param view annotation
// */
//- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
//{
// MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)view.annotation;
// if (tt.shopID) {
// BusinessIfonUVC *BusinessIfonVC = [[BusinessIfonUVC alloc]init];
// BusinessIfonVC.shopId = tt.shopID;
// [self.navigationController pushViewController:BusinessIfonVC animated:YES];
// }
//}
#pragma mark -BMKLocationServiceDelegate
//處理方向變更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
// NSLog(@"heading is %@",userLocation.heading);
[_mapView updateLocationData:userLocation];
}
//處理位置坐標(biāo)更新 (定位到位置是,把位置顯示到地圖正中間,只執(zhí)行一次)
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
// NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//在地圖上更新到用戶所在位置 (只執(zhí)行一次)
if (userLocation.location.coordinate.latitude != 0 || userLocation.location.coordinate.longitude != 0) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_mapView.centerCoordinate = userLocation.location.coordinate;
CLLocationCoordinate2D coor;
coor.latitude = userLocation.location.coordinate.latitude;
coor.longitude = userLocation.location.coordinate.longitude;
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeocodeSearchOption.reverseGeoPoint = coor;
BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];;
if (flag) {
NSLog(@"反地理編碼成功1111111");
}else {
NSLog(@"反地理編碼失敗1111111");
}
});
}
[_mapView updateLocationData:userLocation];
}