調(diào)用百度地圖實(shí)現(xiàn)基本功能

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];
}

map.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末总处,一起剝皮案震驚了整個(gè)濱河市滞项,隨后出現(xiàn)的幾起案子狭归,更是在濱河造成了極大的恐慌,老刑警劉巖文判,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件过椎,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡戏仓,警方通過(guò)查閱死者的電腦和手機(jī)疚宇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)柜去,“玉大人灰嫉,你說(shuō)我怎么就攤上這事∩ど荩” “怎么了讼撒?”我有些...
    開封第一講書人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)股耽。 經(jīng)常有香客問(wèn)我根盒,道長(zhǎng),這世上最難降的妖魔是什么物蝙? 我笑而不...
    開封第一講書人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任炎滞,我火速辦了婚禮,結(jié)果婚禮上诬乞,老公的妹妹穿的比我還像新娘册赛。我一直安慰自己,他們只是感情好震嫉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開白布森瘪。 她就那樣靜靜地躺著,像睡著了一般票堵。 火紅的嫁衣襯著肌膚如雪扼睬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評(píng)論 1 299
  • 那天悴势,我揣著相機(jī)與錄音窗宇,去河邊找鬼。 笑死特纤,一個(gè)胖子當(dāng)著我的面吹牛军俊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捧存,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蝇完,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼官硝!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起短蜕,我...
    開封第一講書人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎傻咖,沒想到半個(gè)月后朋魔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卿操,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年警检,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片害淤。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扇雕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窥摄,到底是詐尸還是另有隱情镶奉,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布崭放,位于F島的核電站哨苛,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏币砂。R本人自食惡果不足惜建峭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望决摧。 院中可真熱鬧亿蒸,春花似錦、人聲如沸掌桩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)拘鞋。三九已至砚蓬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盆色,已是汗流浹背灰蛙。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留隔躲,地道東北人摩梧。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像宣旱,于是被迫代替她去往敵國(guó)和親仅父。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容