app 跳轉到地圖

廢話不說次乓,直接上代碼

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface PositionController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{
    //添加代理協(xié)議 CLLocationManagerDelegate
    CLLocationManager *_locationManager;//定位服務管理類
}
@property (nonatomic, strong)NSString *position;//現(xiàn)在位置 
@property (nonatomic, assign)CLLocationCoordinate2D Coordinate2D;//現(xiàn)在的坐標 
@property (nonatomic, assign)CLLocationCoordinate2D address;//目的地坐標 
@property (nonatomic, strong)NSString *addressName;//目的地名字 
@property (nonatomic, strong)MKMapView *mapView;//地圖
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(self.space, self.gotoBtn.bottom+self.space, WIDTH-2*self.space, HEIGHT-self.gotoBtn.bottom-2*self.space-64)];
        _mapView.backgroundColor=[[UIColor redColor] colorWithAlphaComponent:0.2];
            /**
               MKMapTypeStandard = 0,  // 標準
               MKMapTypeSatellite,     // 衛(wèi)星
               MKMapTypeHybrid,        // 混合(標準+衛(wèi)星)
               MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立體衛(wèi)星
               MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立體混合
            */
        _mapView.mapType = MKMapTypeStandard;//地圖顯示類型
        _mapView.userTrackingMode=MKUserTrackingModeFollow;//添加此句,可以自動定位到當前位置
        _mapView.delegate=self;
        _mapView.showsScale = YES;
        // 是否可以縮放
        _mapView.zoomEnabled = YES;
        // 是否可以滾動
        _mapView.scrollEnabled = YES;
        // 是否可以旋轉
        _mapView.rotateEnabled = YES;
        // 是否顯示3D
        _mapView.pitchEnabled = YES;
    }
    return _mapView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mapView];
    [self  coordinateTouch];
[self performSelector:@selector(gotoTouch) withObject:nil afterDelay:5];
}
- (void)coordinateTouch{
    _locationManager = [[CLLocationManager alloc] init];
    //    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestAlwaysAuthorization];//iOS8必須,這兩行必須有一行執(zhí)行婚夫,否則無法獲取位置信息澄步,和定位
    //    _locationManager.allowsBackgroundLocationUpdates = YES;
    // 設置代理
    _locationManager.delegate = self;
    // 設置定位精確度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 設置過濾器為無
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 開始定位
    [_locationManager startUpdatingLocation];//開始定位之后會不斷的執(zhí)行代理方法更新位置會比較費電所以建議獲取完位置即時關閉更新位置服務
    //初始化地理編碼器
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    //當前位置
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    self.Coordinate2D=oldCoordinate;
    
    [manager stopUpdatingLocation];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *place in placemarks) {         
            self.position = [NSString stringWithFormat:@"%@ %@ %@ %@",place.country,place.locality,place.subLocality,place.thoroughfare];
            [_positionBtn setTitle:self.position forState:UIControlStateNormal];
            break;
        }
    }];
}
- (void)gotoTouch {
    //目的地
    self.addressName = @"杭州濱江區(qū)政府";
    CLGeocoder *geocode = [[CLGeocoder alloc] init];
    
    [geocode geocodeAddressString:self.addressName completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count ] > 0) {
            //移除目前地圖上得所有標注點
            [_mapView removeAnnotations:_mapView.annotations];
        }else {
            NSLog(@"找不到此地址");
            return ;
        }
        
        for (int i = 0; i< [placemarks count]; i++) {
            CLPlacemark * placemark = placemarks[i];
            //調整地圖位置和縮放比例,第一個參數(shù)是目標區(qū)域的中心點裸违,第二個參數(shù):目標區(qū)域南北的跨度呢岗,第三個參數(shù):目標區(qū)域的東西跨度,單位都是米
            MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000);
            
            //重新設置地圖視圖的顯示區(qū)域
            [_mapView setRegion:viewRegion animated:YES];
            // 實例化 MapLocation 對象
            mapLocation * annotation = [[mapLocation alloc] init];
            annotation.streetAddress = placemark.thoroughfare ;
            annotation.city = placemark.locality;
            annotation.state = placemark.administrativeArea ;
            annotation.zip = placemark.postalCode;
            annotation.coordinate = placemark.location.coordinate;
            annotation.title=placemark.name;
            
            self.address=placemark.location.coordinate;;
            //把標注點MapLocation 對象添加到地圖視圖上儒喊,一旦該方法被調用镣奋,地圖視圖委托方法mapView:ViewForAnnotation:就會被回調
            [_mapView addAnnotation:annotation];

//第一個坐標
            CLLocation *current=[[CLLocation alloc] initWithLatitude:self.Coordinate2D.latitude longitude:self.Coordinate2D.longitude];
            //第二個坐標
            CLLocation *before=[[CLLocation alloc] initWithLatitude:self.address.latitude longitude:self.address.longitude];
            // 計算距離
            CLLocationDistance meters=[current distanceFromLocation:before];
            NSLog(@"計算距離========= %.0f 米",meters);
        }
    }];
    
    [self presentViewController:self.alertController animated:YES completion:nil];
}
-(UIAlertController *)alertController{
    if (!_alertController) {
        _alertController = [UIAlertController alertControllerWithTitle:@"請選擇地圖" message:nil
                                                        preferredStyle:UIAlertControllerStyleActionSheet ];
        UIAlertAction *appleAction = [UIAlertAction actionWithTitle:@"蘋果自帶地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            CLLocationCoordinate2D Coordinate2D ;
            Coordinate2D.latitude = self.address.latitude;
            Coordinate2D.longitude = self.address.longitude;
            MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:Coordinate2D addressDictionary:nil]];
            toLocation.name = self.addressName;
            [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
        }];
        UIAlertAction *tecentAction = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=0&style=2",@"住哪兒",@"FXTools",self.addressName,self.address.latitude,self.address.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                
            }
        }];
        UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02",self.address.latitude,self.address.longitude,self.addressName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                [MBProgressHUD showMessage:@"您的手機未安裝百度地圖"];
            }
        }];
        UIAlertAction *tentAction = [UIAlertAction actionWithTitle:@"騰訊地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",self.Coordinate2D.latitude,self.Coordinate2D.longitude,self.address.latitude,self.address.longitude];
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                
            }
        }];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleCancel handler:nil];
        [_alertController addAction:appleAction];
        [_alertController addAction:tecentAction];
        [_alertController  addAction:baiduAction];
        [_alertController  addAction:tentAction];
        [_alertController addAction:cancelAction];
    }
    return _alertController;
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface mapLocation : NSObject<MKAnnotation>
// 地圖標點類必須實現(xiàn) MKAnnotation 協(xié)議
// 地理坐標
@property (nonatomic ,readwrite) CLLocationCoordinate2D coordinate ;
//街道屬性信息
@property (nonatomic , copy) NSString * streetAddress ;
// 城市信息屬性
@property (nonatomic ,copy) NSString * city ;
// 州,省 市 信息
@property(nonatomic ,copy ) NSString * state ;
//郵編
@property (nonatomic ,copy) NSString * zip  ;
@property (nonatomic, copy) NSString *title;
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末怀愧,一起剝皮案震驚了整個濱河市侨颈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芯义,老刑警劉巖哈垢,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異扛拨,居然都是意外死亡耘分,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門绑警,熙熙樓的掌柜王于貴愁眉苦臉地迎上來求泰,“玉大人,你說我怎么就攤上這事计盒】势担” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵北启,是天一觀的道長卜朗。 經常有香客問我拔第,道長,這世上最難降的妖魔是什么聊替? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任楼肪,我火速辦了婚禮培廓,結果婚禮上惹悄,老公的妹妹穿的比我還像新娘。我一直安慰自己肩钠,他們只是感情好泣港,可當我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著价匠,像睡著了一般当纱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上踩窖,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天坡氯,我揣著相機與錄音,去河邊找鬼洋腮。 笑死箫柳,一個胖子當著我的面吹牛,可吹牛的內容都是我干的啥供。 我是一名探鬼主播悯恍,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼伙狐!你這毒婦竟也來了涮毫?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贷屎,失蹤者是張志新(化名)和其女友劉穎罢防,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唉侄,經...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡咒吐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了美旧。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渤滞。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖榴嗅,靈堂內的尸體忽然破棺而出妄呕,到底是詐尸還是另有隱情,我是刑警寧澤嗽测,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布绪励,位于F島的核電站肿孵,受9級特大地震影響,放射性物質發(fā)生泄漏疏魏。R本人自食惡果不足惜停做,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望大莫。 院中可真熱鬧蛉腌,春花似錦、人聲如沸只厘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽羔味。三九已至河咽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赋元,已是汗流浹背忘蟹。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留搁凸,地道東北人媚值。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像坪仇,于是被迫代替她去往敵國和親杂腰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,139評論 25 707
  • 紛繁世界椅文,五彩繽紛喂很,表里斟酌,朦朧難辨皆刺。不想因為想得到想要的東西而失去最不想失去的東西少辣。不想因為想變成想成為的樣子...
    活著就是幸福閱讀 118評論 0 1
  • 我越來越感覺到,做為一個校長羡蛾,我最最最最重要的工作漓帅,就是要哄我的老師們開心。你開心了痴怨,才有班上的孩子們的開心忙干。孩子...
    迪子和我閱讀 156評論 0 0
  • 文/逗逗,圖/花瓣網 1 新品牌新氣象浪藻,再度揚帆起航捐迫。 這幾天的朋友圈,不是很風平浪靜爱葵,連續(xù)幾天施戴,被之前公司同事的...
    遇見逗逗閱讀 149評論 0 0
  • 廢舊的作業(yè)本反浓,黃昏的天際 是誰放飛了紙飛機, 留下了痕跡赞哗。 那是歲月的尾巴雷则,拖著純真的孩子氣, 那是夢想的軌道肪笋,通...
    公子凌希閱讀 324評論 0 9