1. 程式人生 > >IOS 百度地圖定位,顯示地理位置

IOS 百度地圖定位,顯示地理位置

最近公司要做一個類似與美團的東西,讓我用百度地圖來進行定位,並顯示地理資訊.

預覽圖
預覽圖

那麼我們要如何做呢,直接上程式碼

  • 先看看包結構,要把需要用到的庫都要引入進來. 包結構
    包結構
    注意:appdelegate.mm 字尾一定要加個m
    不然的話編譯會不通過,好像是因為它的編譯原理是c++還是怎樣,要詳細瞭解的可以百度.
  • 那麼來看控制器程式碼
    _locService = [[BMKLocationService alloc]init];//定位功能的初始化
    _locService.delegate = self;//設定代理位self
    //啟動LocationService
    [_locService startUserLocationService];//啟動定位服務
_geocodesearch = [[BMKGeoCodeSearch alloc] init]; //編碼服務的初始化(就是獲取經緯度,或者獲取地理位置服務) _geocodesearch.delegate = self;//設定代理為self

這段程式碼需要放在viewDidLoad裡面
啟動定位服務後,_locService.userLocation.location.coordinate就應該會有值了,也就是說經度和緯度都可以獲取出來了.

  • 開始定位的點選事件,將剛剛定位的經緯度取出來並設定lable的值
    -(void)heheda:(UIButton *)btn{
      NSLog
    (@"進入普通定位態"); NSLog(@"定位的經度:%f,定位的緯度:%f",_locService.userLocation.location.coordinate.longitude,_locService.userLocation.location.coordinate.latitude); self.lon.text = [NSString stringWithFormat:@"%f",_locService.userLocation.location.coordinate.longitude]; [self.lat setText:[NSString stringWithFormat:@"%f"
    ,_locService.userLocation.location.coordinate.latitude]]; }
  • 獲取地址的點選事件
-(void)onClickReverseGeocode  //傳送反編碼請求的.
{
    isGeoSearch = false;
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//初始化
    if (_locService.userLocation.location.coordinate.longitude!= 0
        && _locService.userLocation.location.coordinate.latitude!= 0) {
        //如果還沒有給pt賦值,那就將當前的經緯度賦值給pt
        pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude,
            _locService.userLocation.location.coordinate.longitude};
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反編碼請求
    reverseGeocodeSearchOption.reverseGeoPoint = pt;//設定反編碼的店為pt
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//傳送反編碼請求.並返回是否成功
    if(flag)
    {
        NSLog(@"反geo檢索傳送成功");
    }
    else
    {
        NSLog(@"反geo檢索傳送失敗");
    }

}
  • 如果傳送成功,百度將會返回東西給你,然後你可以在下面這個代理函式中處理

    -(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
    {
         if (error == 0) {
          BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
          item.coordinate = result.location;
          item.title = result.address;
          NSString* titleStr;
          NSString* showmeg;
          titleStr = @"反向地理編碼";
          showmeg = [NSString stringWithFormat:@"%@",item.title];
             self.addr.text = showmeg;
          UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定",nil];
          [myAlertView show];
      }
    }
    效果圖
    效果圖


文/區振軒(簡書作者)
原文連結:http://www.jianshu.com/p/dc18c6b5be8b
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

相關推薦

IOS 地圖定位,顯示地理位置

最近公司要做一個類似與美團的東西,讓我用百度地圖來進行定位,並顯示地理資訊. 預覽圖 那麼我們要如何做呢,直接上程式碼 先看看包結構,要把需要用到的庫都要引入進來. 包結構 注意:appdelegate.mm 字尾一定要加個m 不然的話編譯會不通過,好像是因為它的編譯原

地圖定位顯示

//單個地圖顯示 <html> <head>     <title>百度地圖</title>   <script src="http://api.map.baidu.com/api?v=1.4" type="

iOS地圖 定位,獲取當前經緯度

百度地圖定位 #import "ViewController.h" #import <BaiduMapAPI_Location/BMKLocationService.h> @interface ViewController ()<BMKLoca

IOS地圖定位總結

#pragma mark 註冊百度地圖 - (void)reguestBaiduMap {     // 要使用百度地圖,請先啟動BaiduMapManager     mapManager = [[BMKMapManager alloc]init];     BOOL ret = [mapManager

iOS --地圖定位後獲取城市座標,城市名稱,區域名稱

/**  *使用者位置更新後,會呼叫此函式  *@param userLocation 新的使用者位置  */ - (void)didUpdateBMKUserLocation:(BMKU

安卓呼叫地圖定位自己的位置,然後分享自己的位置資訊生成連結短串。

</pre><pre name="code" class="java">         前些天公司讓做一個這樣的功能描述如下 呼叫百度地圖的定位功能,然後通過簡訊將自己的位置

呼叫地圖SDK顯示當前定位位置

根據經緯度顯示位置和地址 public class LocationFragment extends BaseFragment implements View.OnClickListener, OnGetGeoCoderResultListener { @Ove

iOS地圖簡單應用( iOS地圖定位(定位地理編碼與反地理編碼、mapView、大頭針)

匯入百度SDK 注:自iOS8起,系統定位功能進行了升級,SDK為了實現最新的適配,自v2.5.0起也做了相應的修改,開發者在使用定位功能之前,需要在info.plist裡新增(以下二選一,兩個都新增預設使用NSLocationWhenInUseUsageDescrip

微信客戶端利用地圖定位獲取位置資訊

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html

iOS地圖點選BMKPointAnnotation後把該座標點放到螢幕任意位置

由於百度地圖設定螢幕中心點只有一個方法:/// 當前地圖的中心點,改變該值時,地圖的比例尺級別不會發生變化@property (nonatomic) CLLocationCoordinate2D centerCoordinate;但是我們有時可能有需求放到距離任意一點,比如點

iOS地圖,不能實時定位問題

專案中加入了百度地圖,但是發現一個問題,就是有些時候可以實時更新位置,有些時候則不行。然後排查問題,第一次進去的時候可以實時更新位置,然後第二次進去的時候不能實時更新位置,可能有些人已經猜到了問題的所在,就是第一次進去的時候生成的一些東西,在你退出的時候被登出了,然後第二次進

ios 地圖根據路徑規劃自動縮放,顯示完整路線

//根據polyline設定地圖範圍 - (void)mapViewFitPolyLine:(BMKPolyline *) polyLine { CGFloat ltX, ltY, rbX, rbY; if (polyLine.pointCount &

HTML5頁面直接調用地圖API,獲取當前位置,直接導航目的地(轉)

wid dir tle mark utf-8 mil 獲取 open init HTML5頁面直接調用百度地圖API,獲取當前位置,直接導航目的地 我是應用在微信中,自定義菜單,菜單直接鏈接到這個HTML5頁面,獲取當前位置後,頁面中定好目的地,這樣打開頁面後直接進入導航頁

【MUI】地圖定位功能

scom 需要 read rto code lan containe convert marker 博主最近進行一款APP開發,需要用到定位功能,經過一番折騰,終於搞定,不廢話,代碼如下 mui.plusReady(function() {

微信企業號開發:微信考勤地圖定位,錯誤修正二

ipp ons AC 信息 解釋 企業號 android bsp ner 在之前的博客中微信企業號開發:微信考勤百度地圖定位,錯誤修正介紹了一種修正百度地圖錯誤的方法

JavaScript--地圖定位,可拖拽

event 控件 int pla location view api 事件 ont <html> <head> <meta http-equiv="Content-Type" content="text/html; char

地圖定位失敗162問題

http inf bubuko 百度 img 刪掉 。。 .so 程序 今天突然要改程序,發現之前寫的定位的一直返回4.9E-324.百度文檔說是因為.so包的問題,但是看來半天我有.so包啊!為什麽還不行。。。。。沒有編譯 ,一開始我直接復制的文檔裏的這個放上去後還是不行

HTML5頁面直接調用地圖API,獲取當前位置,直接導航目的地

position new html5 head css target str direction lmap <!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"

js使用地圖顯示中國區域,實現大資料熱點圖

需求:領導需要在年會上展示我們的使用者ip實時資料,做一個網頁版的地圖,僅僅顯示中國區域。 技術分析:echart,hchart等網站都有地圖版的,百度地圖有熱點例項,經過對比,我選用了百度地圖,但是百度地圖無法只顯示中國區域,這個時候就需要我來動動手解決掉最後一關。 應評論的各位想要原始碼

加載地圖之後 中心為位置有點偏移

func tiles listener loaded tlist ade nbsp poi load //解決位置偏移var loadCount = 1; map.addEventListener("tilesloaded",function() {