根據輸入的地址跳轉到百度地圖或者高德地圖進行路線導航

寫在前面: 盡管精確度已然調至最佳, 但還是會有一定程度的定位誤差, 作為開發(fā)者我已然盡力, 只能希望apple官方做些優(yōu)化吧

輸入目的地地址時最好填入區(qū), 不然有時會定位不到
http://developer.baidu.com/map/wiki/index.php?title=uri/api/ios
這個網址是百度地圖官方的, 提供web和ios跳轉到百度地圖app的各種url, 諸如可以輸入起點終點進行導航, 或者把輸入的地址顯示在百度地圖上等等

下面這個對地址反編譯成經緯度就已經要用到了, 更別說后面的定位了
#import <CoreLocation/CoreLocation.h>

要調用自帶的高德地圖, 就要
#import <MapKit/MapKit.h>

我的demo控件只是創(chuàng)建了一個開始跳轉到地圖的按鈕和輸入目的地的textField而已

以下是屬性

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *buttonOfBeginLocate;

//目的地經緯度
@property (nonatomic, assign) CGFloat longitude;
@property (nonatomic, assign) CGFloat latitude;

//目前所在地經緯度
@property (nonatomic, assign) CGFloat currentLatitude;
@property (nonatomic, assign) CGFloat currentLongitude;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geocoder;

寫倆懶加載

 #pragma mark - 懶加載
- (CLLocationManager *)locationManager{

    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        // 設置定位精確度到米
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 設置過濾器為無
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 一個是requestAlwaysAuthorization厨相,一個是requestWhenInUseAuthorization
        [_locationManager requestWhenInUseAuthorization];//這句話ios8以上版本使用。

    }
    return _locationManager;
}

- (CLGeocoder *)geocoder{

    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

在button點擊事件里

  - (void)beginLocate:(UIButton *)button{

   [self.locationManager startUpdatingLocation];

   [self translateAddress];

   //設備安裝了百度地圖
  if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
      [self dumpToBaidu];
  }
  //沒安裝百度地圖, 跳轉到自帶的高德
  else{
      [self testAppleMapWithLatitude:_latitude longitude:_longitude];
  }
}


#pragma mark - 定位協(xié)議方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    _currentLatitude = newLocation.coordinate.latitude;
    _currentLongitude = newLocation.coordinate.longitude;
    if (_currentLatitude && _currentLongitude) {
        [manager stopUpdatingLocation];
    }

}


#pragma mark - 目的地地址編譯為經緯度. 
//地址盡量有區(qū), 比如龍湖區(qū)
- (void)translateAddress{

    [self.geocoder geocodeAddressString:_textField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
        if (placemarks.count > 0 && error == nil) {
        
            CLPlacemark *placemark = placemarks.firstObject;
        
            _longitude = placemark.location.coordinate.longitude;
            _latitude = placemark.location.coordinate.latitude;
        
        }
        else if (placemarks.count == 0 && error == nil){
            NSLog(@"placemarks元素為0");
        }else if(error != nil){
            NSLog(@"an arror occurred = %@", error);
        }
    }];

}

#pragma mark - 跳轉到百度地圖
- (void)dumpToBaidu{

    //轉成UTF8  [NSCharacterSet URLQueryAllowedCharacterSet]
    //四個參數分別是, 當前位置緯度, 經度, 目的地緯度, 經度
    NSString *url4 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=driving&src=webapp.navi.yourCompanyName.yourAppName", _currentLatitude, _currentLongitude, _latitude, _longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    //根據起點終點跳轉到百度地圖并進行駕車導航
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url4]];

}

#pragma mark - 跳轉到蘋果高德地圖
-(void)testAppleMapWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{

    //    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(30.691793,104.088264);

    //    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(30.691293,104.088264);


    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(_currentLatitude, _currentLongitude);

//    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(40.001,116.404);

    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(latitude, longitude);

    //這個判斷我沒試過, 現(xiàn)在也沒幾個用ios6了吧
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0){
    
        // ios6以下嗽测,調用google map {
    
        NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", coords1.latitude,coords1.longitude,coords2.latitude,coords2.longitude];
    
        NSURL *aURL = [NSURL URLWithString:urlString]; //打開網頁google地圖
    
        [[UIApplication sharedApplication] openURL:aURL];
    
        }else// 直接調用ios自己帶的apple map
    
    {
    
        //當前的位置
    
        //    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    
        //起點
    
        MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
    currentLocation.name = @"目前位置";
    
    
        //目的地的位置
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
    
        //顯示在地圖上的目的地名稱
        toLocation.name = @"目的地";
    
        NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
    
        NSDictionary *options = @{
                              MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES
                              }; //打開蘋果自身地圖應用撑帖,并呈現(xiàn)特定的item
    
        [MKMapItem openMapsWithItems:items launchOptions:options];
    
    }

}

最后感謝 http://blog.csdn.net/hengshujiyi/article/details/45560609 給我靈感

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末蓉坎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子胡嘿,更是在濱河造成了極大的恐慌蛉艾,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異勿侯,居然都是意外死亡拓瞪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門罐监,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吴藻,“玉大人,你說我怎么就攤上這事弓柱。” “怎么了侧但?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵矢空,是天一觀的道長。 經常有香客問我禀横,道長屁药,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任柏锄,我火速辦了婚禮酿箭,結果婚禮上,老公的妹妹穿的比我還像新娘趾娃。我一直安慰自己缭嫡,他們只是感情好,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布抬闷。 她就那樣靜靜地躺著妇蛀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪笤成。 梳的紋絲不亂的頭發(fā)上评架,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天,我揣著相機與錄音炕泳,去河邊找鬼纵诞。 笑死,一個胖子當著我的面吹牛培遵,可吹牛的內容都是我干的浙芙。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼荤懂,長吁一口氣:“原來是場噩夢啊……” “哼茁裙!你這毒婦竟也來了?” 一聲冷哼從身側響起节仿,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤晤锥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體矾瘾,經...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡女轿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了壕翩。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛉迹。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖放妈,靈堂內的尸體忽然破棺而出北救,到底是詐尸還是另有隱情,我是刑警寧澤芜抒,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布珍策,位于F島的核電站,受9級特大地震影響宅倒,放射性物質發(fā)生泄漏攘宙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一拐迁、第九天 我趴在偏房一處隱蔽的房頂上張望蹭劈。 院中可真熱鬧,春花似錦线召、人聲如沸铺韧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祟蚀。三九已至,卻和暖如春割卖,著一層夾襖步出監(jiān)牢的瞬間前酿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工鹏溯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留罢维,地道東北人。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓丙挽,卻偏偏與公主長得像肺孵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子颜阐,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫平窘、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,124評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,326評論 25 707
  • 昨晚夜班凳怨,見到寶貝已經九點多了瑰艘,在外婆家是鬼,昨晚表現(xiàn)不是很好,把床尿濕了紫新,和外婆接觸比較少點均蜜,寶貝應該是有點不適應的...
    親然閱讀 250評論 0 0
  • 我是家里的老小,我從小是家里被照顧的那個芒率,用伯父他們的話說囤耳,但我從來沒有覺得自己是被照顧的,我只是不需要去干農...
    王翠英閱讀 248評論 0 0
  • CGAffineTransform是一個映射轉換3*3的矩陣偶芍,用來繪畫2D圖像充择。可以實現(xiàn)放大匪蟀、縮小聪铺、平移。先看看其...
    yuandiLiao閱讀 9,220評論 0 10